You are on page 1of 8

COMP232 Fundamentals of Programming

Page 1 of 8

COMP232 Fundamentals of Programming


Semester II 20112012 Midterm Test

Suggested Solutions

1. Choose the correct answers (48 marks, 4 marks each) (1) Which of the following binary numbers is equal to 563? (A) 1001110111 (B) 1001110011 (C) 1000110011 (D) 1100110001

Ans: C
(2) What will be printed by the following program statement? System.out.println( 9/4 + 3/4 ); (A) 2 (B) 3.0 (C) 3 (D) 20

Ans: A
(3) Given that i is an int variable; c is a char variable; f is a float variable. What is the data type for the result for the following expression? f*(3.14*i+c) (A) char (B) int (C) float (D) double

Ans: D
(4) What will be the values of variables i and d after executing the following program statements? int i; double d=2.0; i=(int)(d*2.2); (A) i=4, d=2.0 (B) i=4.4, d=2.0 (C) i=4.4, d=4.4 (D) i=4, d=4.4

Ans: A

COMP232 Fundamentals of Programming

Page 2 of 8

(5) What will be the values of variables a, b and x after executing the following program statements? int a,b,x; a=b=x=1; b = ++a; x = ++a - b--; (A) a=2, b=2, x=2 (B) a=3, b=2, x=2 (C) a=3, b=1, x=2 (D) a=3, b=1, x=1

Ans: D
(6) What will be the values of variables x, a and b after executing the following program statements? boolean x, a=false, b=false, c=true; x = (a != (b = c)); (A) x=true, a=true, b=false (C) x=false, a=true, b=true (B) x=false, a=false, b=false (D) x=true, a=false, b=true

Ans: D
(7) What will be the value of variable x after executing the following program statements? int x=0, y=12; for (int i=0; i<=y; i+=4) x++; (A) x=2 (B) x=3 (C) x=4 (D) x=5

Ans: C
(8) What will be the value of variable x after executing the following program statements? int x, a=1; for (x=1; x<9; x++) { a++; if (a == 7) break; } (A) x=3 (B) x=4 (C) x=5 (D) x=6

Ans: D

COMP232 Fundamentals of Programming

Page 3 of 8

(9) What will be the value of variable x after executing the following program statements? int x, y, a=1; Stop: for (x=1; x<9; x++) for (y=1; y<3; y++) { a++; if (a == 7) break Stop; } (A) x=3 (B) x=4 (C) x=5 (D) x=6

Ans: A
(10) What is the output when the following method is executed? public static void M() { boolean b = true; if (b == false) { System.out.print("A"); } else if (b) { System.out.print("B"); } else { System.out.print("C"); } } (A) A (B) B (C) C (D) none of the above

Ans: B
(11) What will be printed by executing the program below? public class Call { public static void main(String [] args) { int x=1; x = mod4(x); x = mod4(x); x = mod4(x); System.out.println(x); } public static int mod4(int x) { return (x+1)%4; } } (A) 0 (B) 1 (C) 2 (D) 3

Ans: A

COMP232 Fundamentals of Programming

Page 4 of 8

(12) What is the output when the following method is executed? public static void M() { for (int i=1; i <= 6; i++) { for (int j=1; j <= 6; j++) { if ( i== 1 || i == 6) System.out.print("*"); else if ( j == 7-i ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } (A) ****** * * * * ****** (B) ****** * * * * ****** (C) ****** * * * * * * * * ****** (D) ****** * * ** ** * * ******

Ans: B

2. Please provide the correct answers (24 marks, 6 marks each) (1) If x stores 5, and y stores 5, what will be printed after executing the following program statements? if (x>=2) if (y<=4) System.out.println(x); else System.out.println(y); System.out.println(x+y);

Ans: 5 10

COMP232 Fundamentals of Programming

Page 5 of 8

(2) What will be printed by executing the program below? public class A { public static void main(String [] args) { int size = 4; for (int i=1; i <= size; i++) { for (int j=1; j <= size-i; j++) System.out.print(" "); for (int j=1; j <= i; j++) System.out.print("+"); System.out.println(); } for (int i=1; i <= size-1; i++) { for (int j=1; j <= i; j++) System.out.print(" "); for (int j=1; j <= size-i; j++) System.out.print("+"); System.out.println(); } } }

Ans: + ++ +++ ++++ +++ ++ +

COMP232 Fundamentals of Programming

Page 6 of 8

(3) What will be printed by executing the program below? public class A { public static void main(String [] args) { int num=5; for (int i=1; i<=num; i++) { for (int j=1; j<=(num-i); j++) System.out.print(" "); for (int k=1; k<=i; k++) System.out.print(k); System.out.println(); } } }

Ans: 1 12 123 1234 12345

(4) What will be printed by executing the program below? public class A { public static void main(String [] args) { int x = 3, y = 5; xMethod(x, y); System.out.println("Output: " + (x + y)); } public static void xMethod(int x, int y) { x += y; } }

Ans: Output: 8

COMP232 Fundamentals of Programming

Page 7 of 8

3. (8 marks) Write a method sumSeries() to compute the summation of the following series:

m(i) = The method signature is given below:

1 + 2

2 + + 3

i i+1

public static double sumSeries(int num)

The method takes an integer parameter num and returns the summation of the series m(num).

Ans: public static double sumSeries(int num) { double sum = 0; for ( int i=1; i<=num; i++) sum += Math.sqrt(i/(i+1.0)); return sum; }

4. (8 marks) Write a recursive method findLargestDigit() whose signature is given below: public static int findLargestDigit(int num)

The method accepts an integer parameter num and returns the the digit which is the largest one of the integer. For example, the method call findLargestDigit(245804) returns 8. Please complete the method findLargestDigit().

Ans: public static int findLargestDigit(int num) { if (num/10 == 0) return num; else return num%10 > findLargestDigit(num/10) ? num%10 : findLargestDigit(num/10); }

COMP232 Fundamentals of Programming

Page 8 of 8

5. (12 marks) Write a program to nd an integer between 1 and 10000 which has the largest number of divisors. When the integer is found, the program prints out the integer and all its divisors. It is possible that several integers in this range have the same maximum number of divisors. Your program has to print out the smallest one among them.

Ans: public class A { public static void main(String [] args) { int maxNum = 1; int maxInt = 1; for (int i = 1; i <= 10000; i++) { int count = 0; for (int j = 1; j <=i; j++) { if (i%j == 0) count ++; } if (count > maxNum) { maxNum = count; maxInt = i; } } System.out.println("The integer is " + maxInt); System.out.print("All divisors are: "); for (int i = 1; i<= maxInt; i++) if (maxInt%i == 0) System.out.print(i + " "); System.out.println("\n"); } }

-End-

You might also like