Sie sind auf Seite 1von 6

Faculty of Computing Studies (FCS)

Course Code: TM105 / M105


Course Title: Introduction to Computer
Programming

Mid-term – MOCK

Total Marks: 60

Number of Exam Pages (6) Time Allowed: (90 Minutes)


(including this cover sheet)

Exam Instructions:
• Total Number of Question is 5 totally of 60 marks.
• ALL questions must be answered in the External Answer booklet.
• Be sure you write your name and ID on the External Answer booklet.
• Go through each and every question before you start answering it. Note that the
estimated time for each question is mentioned in the question header.
• Place your question paper in the answer booklet and give them to the invigilator.
• Student handbooks are NOT permitted in this examination.
• Electronic devices that could have a memory are NOT permitted in this
examination.
Part I - Multiple Choice Questions (10 Marks)
This part consists of 6 multiple choice questions. For each question, encircle the label A, B, C,
or D as your most appropriate answer. Each question carries 2 points.

1. ________ language is a machine dependent language?


a. Machine language b. Assembly
c. Java d. a or b

2. Which of the following is not a valid identifier in Java?


a. MTA_1_Makeup b. MTA1MAKEUP
c. MTA$1$Makeup d. None of the above

3. What is the value of x after executing the following statement?


int x = (int) Math.ceil(1.2 * 1.2);
a. 1.0 b. 1
c. 2.0 d. 2

4. What is the value of z after executing the following statement?


int z = (7%3 == 2)? 10:50;

a. 10 b. 50
c. 1 d. 1050

5. What is the value of x and y after executing the following statements?


int x = 10; int y = 9; int z=++x* y--;
a. x = 11, y = 8, z=88 b. x = 10, y = 9, z=99
c. x = 10, y = 8, z=99 d. x = 11, y = 8, z=99;

TM105 - MTA MOCK Page 2 of 6


Question 2: (12 Marks)
a. Rewrite the following mathematical expressions in Java: (6 Marks; 2 marks for each)
3
√𝑎−5
i.
3+2𝑏
Math.pow((a-5), (1.0/3.0))/(3+2*b)
5
ii. 𝑐𝑜𝑠 | |
𝑥
Math.cos(Math.abs(5/x)

iii. 𝑙𝑛(4 + 𝑒𝜋)


Math.log(4 + Math.E * Math.PI))

If student dealt with e as a variable name not the natural Logarithm


constant, an accepted answer could be:
Math.log(4 + e * Math.PI))

b. Evaluate the following expression by determining the operator precedence and


calculating the final value when 𝑥 = 8, 𝑦 = 4: (3 Marks)
𝒙 − 𝒚 ∗ 𝒚 + (𝒙 − 𝟐) ∗ (𝒚 − 𝒙)

ANSWER: 0.5 Mark for each operation

𝒙 − 𝒚 ∗ 𝒚 + (𝒙 − 𝟐) ∗ (𝒚 − 𝒙)
=6 1

𝒙 − 𝒚 ∗ 𝒚 + 𝟔 ∗ (𝒚 − 𝒙)
=-4 2

𝒙 − 𝒚 ∗ 𝒚 + 𝟔 ∗ (−𝟒)
= 16 3

𝒙 − 𝟏𝟔 + 𝟔 ∗ (−𝟒)
= - 24 4

𝒙 − 𝟏𝟔 − 𝟐𝟒
=-8 5

− 𝟖 − 𝟐𝟒
= - 32 6

TM105 - MTA MOCK Page 3 of 6


c. Rewrite the following piece of code substituting switch with if control: (3 Marks)
int number = 5;
switch (number) {
case 1: System.out.println("AAA"); break;
case 2: System.out.println("BBB"); break;
case 3:
case 4: System.out.println("CCC or DDD"); break;
default: System.out.println("out of the period!");
}

ANSWER:
if(number = = 1) //0.5 Mark
System.out.println("AAA");
else if(number = = 2) //0.75 Mark
System.out.println("BBB");
Else if(number = = 3 || number = = 4) //1.25 Mark
System.out.println("CCC or DDD");
else //0.5 Mark
System.out.println("out of the period!");

Question 3: (12 Marks)


a. Trace the following pieces of code and give their exact output. [6 marks]
i. String s1 = "M105 Makeup";
String s2 = "TM105";
System.out.printf("%c%s\n",s1.charAt(8),s2);
ANSWER: 1 Mark for “8” and 1 mark for “TM105”
eTM105
ii.
int a = 10, b = 7;
if(a != b)
System.out.println("Different");
if(a == b);
System.out.println("Equal");
ANSWER: 1 Mark for each line
Different
Equal

iii. int c = 70;


switch (c+=10) {
case 70: System.out.println("Seventy"); break;
case 79: System.out.println("Eighty"); break;
case 89: System.out.println("THIRD"); break;
default: System.out.println("???");
}
System.out.println("Now, c = " + c);
ANSWER: 1 Mark for each line
???
Now, c = 80

TM105 - MTA MOCK Page 4 of 6


b. The following piece of code computes the square root of an integer a. This code has two
errors; detect the errors, determine the type of each error and rewrite the code
correctly. (6 Marks)
int a = 10;
int b = input.nextDouble();
System.out.println("a + b = " + a + b);

ANSWER: 3 marks for each error, 1 mark for detecting the error,1 mark for the
error type and 1 mark for the correction
Error 1: Syntax (compilation) error; using nextDouble to read an integer value
Error 2: logic error; using + in the println statement without surrounding a+b with
parantheses beacuase this will print the value of a beside the value of b not the sum value.
int a = 10;
int b = input.nextInt();
System.out.println("a + b = " + (a + b) );

Question 4: (12 Marks)


Write a Java class called Hexagonal that computes the area and s
perimeter of a hexagonal shape based on the hexagonal side s. The area
and perimeter are computed using the formulas:
𝟑√𝟑 𝟐
𝒂𝒓𝒆𝒂 = 𝒔 𝒑𝒆𝒓𝒊𝒎𝒆𝒕𝒆𝒓 = 𝟔𝒔
𝟐
a. Reads from the user a real value representing s.
b. Calculates and prints the distance area (rounded to 2 decimal places) and
perimeter (rounded to 2 decimal places) of the hexagonal.
Sample Run:
Enter the hexagonal side: 12.5
Area = 405.95
Perimeter = 75.00
ANSWER
import java.util.Scanner; //1 mark
public class Hexagonal { //1 mark
public static void main(String[] args) { //1 mark
Scanner input = new Scanner(System.in); //1 mark
double s, arae, perimeter; //1.5 mark
System.out.print("Enter the hexagnal side: "); //0.5 mark
s = input.nextdouble(); //1 mark
area = 3 * Math.sqrt(3.0) / 2.0 * s * s; //2 mark
perimeter = 6 * s; //1 mark
System.out.printf("Area = %.2f\n", area); //1 marks
System.out.printf("Perimeter = %.2f\n", perimeter); //1 marks
}
}

Question 5: (15 Marks)


Body Mass Index (BMI) is a measure of health on weight. BMI is computed as a function of the
weight in kilograms w and the height in meters h using the formula: 𝑩𝑴𝑰 = 𝒘/𝒉𝟐 .
The category of weight case is determined according to the following table:

TM105 - MTA MOCK Page 5 of 6


BMI value Category
less than 18.5 underweight
18.5 – 25 ideal
25 – 30 overweight
Greater than 30 obese

Write a java program the does the following:


• Reads from the user the weight and the height of a person.
• Calculate and prints (rounded to 2 decimal places) the value of BMI.
• Determine the category of weight state according to the given table.

Sample Run:
Enter your weight (kilograms): 110
Enter your height (meters): 1.75
Your BMI value = 35.92
You are OBESE

ANSWER:
import java.util.Scanner; //1 Mark
public class BMI {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //1 Mark
System.out.println("Enter your weight (kilograms): "); //0.5 Mark
double w = input.nextDouble(); //1 Mark
System.out.println("Enter your height (meters): "); //0.5 Mark
double h = input.nextInt(); //1 Mark
double bmi = w / (h * h); //1.5 Mark
System.out.printf(" Your BMI value = %.2f\n", bmi); //1 Mark
if(bmi < 18.5) //1 Mark
System.out.printf("You are UNDERWEIGHT”); //1 Mark
else if(bmi <= 25) //1 Mark
System.out.printf("You are IDEAL”); //1 Mark
else if(bmi <= 30) //1 Mark
System.out.printf("You are OVERWEIGHT”); //1 Mark
else //0.5 Mark
System.out.printf("You are OBESE”); //1 Mark
}
}

– End of Exam –

TM105 - MTA MOCK Page 6 of 6

Das könnte Ihnen auch gefallen