Monday, July 14, 2008

try..catch..finally SCJP 6 Sample Exam Questions (kewords & flow control too!)

Given the following Java code, what would be the result of compling and running the class?

public class JavaCertified extends Object {

public static void main(String args[]) {

long bow = 99;
int x = 100;

boolean flag = true;
boolean switch = false;

if (!flag) System.out.print(flag);
if (!switch) System.out.print(switch);

if (bow < x) {System.out.print("less");} else {System.out.print("more");}

}

}

a) The console would print trueless
b) The console would print falseless
c) The console would print truemore
d) the console would print falsemore
e) The code would not compile
f) The code would compile but not run


Option f) is the correct answer to this question.

The code will not run due to the fact that the keyword swtich is being used as a varible name.



***
Given the following Java code, what would be the result of compling and running the class?


public class JavaCertified extends Object {

public static void main(String args[]) {

long bow = 99;
int x = 100;

boolean flag = true;
boolean onSwitch = false;

if (!flag) System.out.print(flag);
if (!onSwitch) System.out.print(onSwitch);

if (bow < x) System.out.print("less"); else System.out.print("more");

}

}

Given the following Java code, what would be the result of compling and running the class?

public class JavaCertified extends Object {

public static void main(String args[]) {

long bow = 99; int x = 100;

boolean flag = true; boolean onSwitch = false;

if (!flag) System.out.print(flag);
if (!onSwitch) System.out.print(onSwitch);

if (bow < x) System.out.print("less"); else System.out.print("more");

}

}

a) The console would print trueless
b) The console would print falseless
c) The console would print truemore
d) the console would print falsemore
e) The code would not compile
f) The code would compile but not run

In this case, option b) is correct.

There are no tricks in this particular question, although the code, missing some of the formatting style that we expect from Java programs, looks a little bit peculiar. Nevertheless, the question is testing you on your ability to use the not operator, !, along with the ability to evaulate basic comparison operators, in this case, less than, <.

Since onSwitch is false, !onSwitch evaluates to true, so the value of onSwitch is printed to the console.

Furthermore, 99 is less than 100, so the word less is printed to the console after the word false.

Although the if..else block does not contain braces, as it really should in any good Java program, the fact remains that if the braces do not exist after an if or else block, the first, and only first, statement after the block is considered to make up the entire body of the block.

Sometimes, just due to the fact that the Sun Certification exam contents gets mangled as they go through various internationalizations, the syntax and formatting of the code will often violate Suns very own 'suggested coding standards.' Don't let non-standard formatting throw you off. If the code is good, and the syntax is solid, just concentrate on the contents of the question and answer it correctly.

No comments: