Q1. Given the code fragment:
for (int ii = 0; ii < 3;ii++) {
int count = 0;
for (int jj = 3; jj > 0; jj--) {
if (ii == jj) {
++count;
break;
}
}
System.out.print(count); continue; }
What is the result?
A. 011
B. 012
C. 123
D. 000
Answer: A
Q2. Which two items can legally be contained within a java class declaration?
A. An import statement
B. A field declaration
C. A package declaration
D. A method declaration
Answer: B,D
Reference:
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
Q3. Given the content of three files:
Which statement is true? Which statement is true?
A. Only the A.Java file compiles successfully.
B. Only the B.java file compiles successfully.
C. Only the C.java file compiles successfully.
D. The A.Java and B.java files compile successfully.
E. The B.java and C.java files compile successfully.
F. The A.Java and C.java files compile successfully.
Answer: A
Explanation: In class B.Java doStuff() has access modifier with variable name which is not allowed. C.Java class name is different than file name. Only private classes can have different names than file names
Q4. Given the code fragment:
What is the result?
A. A B C
B. A B C D E
C. A B D E
D. Compilation fails.
Answer: C
Q5. Given the code fragment:
Which three lines fail to compile?
A. Line 7
B. Line 8
C. Line 9
D. Line 10
E. Line 11
F. Line 12
Answer: A,D,F
Q6. Given:
What is the result?
A. Base DerivedA
B. Base DerivedB
C. DerivedB DerivedB
D. DerivedB DerivedA
E. A classcast Except ion is thrown at runtime.
Answer: C
Q7. Class StaticField {
static int i = 7;
public static void main(String[] args) {
StaticFied obj = new StaticField();
obj.i++;
StaticField.i++;
obj.i++;
System.out.println(StaticField.i + " "+ obj.i);
}
}
What is the result?
A. 10 10
B. 8 9
C. 9 8
D. 7 10
Answer: A
Q8. Given:
public class SampleClass {
public static void main(String[] args) {
AnotherSampleClass asc = new AnotherSampleClass(); SampleClass sc = new
SampleClass();
sc = asc;
System.out.println("sc: " + sc.getClass());
System.out.println("asc: " + asc.getClass());
}}
class AnotherSampleClass extends SampleClass {
}
What is the result?
A. sc: class Object asc: class AnotherSampleClass
B. sc: class SampleClass asc: class AnotherSampleClass
C. sc: class AnotherSampleClass asc: class SampleClass
D. sc: class AnotherSampleClass asc: class AnotherSampleClass
Answer: D
Q9. Given:
What is the result?
A. 120
B. 120D
C. A NumberFormatException will be thrown.
D. Compilation fails due to error at line 5.
E. Compilation tails due to error at line 8.
Answer: E
Explanation:
At line 5, we have created a wrapper object of double by passing 120D, which is convertible to a Double, so there won't be any exception there. But if you check carefully, you can see the variable number is declared inside try block, so the scope of the variable number is limited to that block, so trying to access it outside causes a compile time error. httpsy/docs.oracle.com/javase/tutorial/iava/nutsandbolts/variables.html
Q10. Given:
public class ColorTest {
public static void main(String[] args) {
String[] colors = {"red", "blue","green","yellow","maroon","cyan"};
int count = 0;
for (String c : colors) {
if (count >= 4) {
break;
}
else {
continue;
}
if (c.length() >= 4) {
colors[count] = c.substring(0,3);
}
count++;
}
System.out.println(colors[count]);
}
}
What is the result?
A. Yellow
B. Maroon
C. Compilation fails
D. A StringIndexOutOfBoundsException is thrown at runtime.
Answer: C
Explanation: The line, if (c.length() >= 4) {, is never reached. This causes a compilation error.
Note: The continue statement skips the current iteration of a for, while , or do-while loop. An unlabeled.break.statement terminates the innermost.switch,.for,.while, or.do-while.statement, but a labeled.break.terminates an outer statement.
Q11. Given the following classes:
Which two options fail to compile when placed at line n1 of the main method?
A. employee.salary = 50_000;
B. director.salary = 80_000;
C. employee.budget = 200_000;
D. manager.budget = 1_000_000;
E. manager.stockOption = 500;
F. director.stockOptions = 1_000;
Answer: C,E
Q12. Given the following two classes:
How should you write methods in the ElectricAccount class at line n1 so that the member variable bill is always equal to the value of the member variable kwh multiplied by the member variable rate?
Any amount of electricity used by a customer (represented by an instance of the customer class) must contribute to the customer's bill (represented by the member variable bill) through the method useElectricity method. An instance of the customer class should never be able to tamper with or decrease the value of the member variable bill.
A. Option A
B. Option B
C. Option C
D. Option D
Answer: B
Q13. Given the code fragment:
What is the result?
A. Values are : [EE, ME]
B. Values are : [EE, EE, ME]
C. Values are : [EE, ME, EE]
D. Values are : [SE, EE, ME, EE]
E. Values are : [EE, ME, SE, EE]
Answer: E
Q14. Given:
What is result?
A. Successful
B. Unsuccessful
C. Compilation fails
D. An exception is thrown at runtime
Answer: C
Q15. Given the code fragment:
What is the result?
A. 28false29 true
B. 285 < 429 true
C. true true
D. compilation fails
Answer: C