CIM/Prog. ITest. 1

DEPARTMENTS OF

COMPUTING AND INFORMATION MANAGEMENT

Programming I Test I

Date: 14/04/2005

Name:Class:41202/1Group No.: Marks:

Totally 60 minutes are allowed to attempt ALL of the following questions.

Write youranswers on the question paper.All working must be clearly shown.

1.State whether the following are valid switch statements.

If not, explain why.

Assume that n and digit are int variables.

(a.)

switch(n<=1){

case 0: System.out.println(“Draw.”);

break;

case 1: System.out.println(“Win.”);

break;

case 2: System.out.println(“Lose.”);

break;

}(2 marks)

(b.)

switch(digit/2){

case 0: case 1:System.out.println(“low.”);

break;

case 1: case 1:System.out.println(“middle.”);

break;

case 3: System.out.println(“high.”);

}(2 marks)

(c.)

switch(n%6){

case 1: case 2: case 3:System.out.println(n);

break;

case 0:System.out.println();

break;

default:System.out.println(“Cannot find in the statements.”);

}(1 marks)

Ans:

(a)is invalid: The expression n <= 1 evaluates to a boolean value, which is not an integral type. The expression in the switch expression must evaluate to an integral value.

(b)Invalid; A case value cannot appear more than once.

(c) valid.

  1. The following program contains errors. Correct them on the statements so that the program will run and output w = 22.

public class Wrong {

static int ONE = 5;

public static main(String[] args) {

int x, y, w, z

z = 10;

if(z >= 10)

{

x = 12; y = 5; w = x + y + ONE;

}

{

x = 12; y = 4; w = x + y + ONE;

}

System.out.println(w = + w);

}

}(5 marks)

Ans:

public class Wrong

{

static final int ONE = 5;

public static void main(String[] args)

{

int x, y, w, z;

z = 10;

if(z >= 10)

{

x = 12; y = 5; w = x + y + ONE;

}

else

{

x = 12; y = 4; w = x + y + ONE;

}

System.out.println("w = " + w);

}

}

3.Rewrite the following decision statement using the conditional operator ( ? : ).

if (num > 10)

System.out.println (“This number is greater than 10.”);

else

System.out.println (“This number is smaller than 10.”); (5 marks)

Ans:

System.out.println(“This number is” + (num>10 ? “greater than” : “smaller than ”) + “10.”);

4.Given the following program segment

j=2;

for(i=1; i<=5; i++) {

System.out.print(j + “ ”);

j = j+5;

}

System.out.println();

Write a while and a do…while loop that have the same output.(6 marks)

Ans:

j = 2;

i = 1;

while(i <= 5){

System.out.print(j + " ");

j = j + 5;

i++;

}

System.out.println();

b.

j = 2;

i = 1;

do{

System.out.print(j + " ");

j = j + 5;

i++;

} while(i <= 5);

System.out.println();

  1. Assume that the following code is correctly inserted into a program:

int s = 0;

int i;

for(i=0; i<3; i++)

{

s = 2 * s + i;

System.out.print(s + “ ”);

}

System.out.println();

(a)What is the final value of s?(2 marks)

(b)If a semicolon is inserted after the right parentheses in the for loop control expressions, what is the final value of s? (2 marks)

(c)If the 3 in item (b) is replaced with a 0 in the for loop control expression, what is the final value of s? (2 marks)

Ans:

(a)4

(b)3

(c)0

  1. Which of the following method headings are valid? If they are invalid, explain why.

(a)public static one(int a, int b)(2 marks)

(b)public static int thisOne(char x)(1 marks)

Ans:

a. Invalid; Method type is missing.

  1. Valid.

7.If x=5, y=6, z=4, and w=3.5, evaluate each of the following statements, if possible.

  1. (x+z)%y3
  2. (x+y)%wnot possible
  3. (y+w)%xnot possible
  4. (x+y)*w38.5
  5. (x%y)%z1

8.Consider the following statements:

String str = “Going to the amusement park.”;

char ch;

int len;

int position;

  1. What value is stored in ch by the following statement?

ch = str.charAt(10);

The value stored in ch is ‘G’.

  1. What value is stored in ch by the following statement?

ch = str.charAt(0);

The value stored in ch is ‘h’.

  1. What value is stored in len by the following statement?

len = str.length();

The value stored in len is28.

9.Suppose that str is a String variable. Write a Java statement that uses the operator new to instantiate the object str and assign the string “Java Programming” to str.

Ans:String str = new String (“Java Programming”);

10.What is the output of the following Java code?

x=100;

y=200;

if(x>100 & y<=200)

System.out.println(x + “ ” + y + “ ” + (x+y));

Else

System.out.println(x + “ ” + y + “ ” + (2*x-y));Ans: 100 200 0

*** END OF TEST ***

Page 1 of 5