Q1. Given:
What is the result?
A. C B A
B. C
C. A B C
D. Compilation fails at line n1 and line n2
Answer: C
Q2. Given:
A. X XX
B. X Y X
C. Y Y X
D. Y YY
Answer: D
Q3. Given:
public class TestField {
int x;
int y;
public void doStuff(int x, int y) {
this.x = x;
y =this.y;
}
public void display() {
System.out.print(x + " " + y + " : ");
}
public static void main(String[] args) {
TestField m1 = new TestField();
m1.x = 100;
m1.y = 200;
TestField m2 = new TestField();
m2.doStuff(m1.x, m1.y);
m1.display();
m2.display();
}
}
What is the result?
A. 100 200 : 100 200
B. 100 0 : 100 0 :
C. 100 200 : 100 0 :
D. 100 0 : 100 200 :
Answer: C
Q4. Given the following four Java file definitions:
// Foo.java
package facades;
public interface Foo { }
// Boo.java
package facades;
public interface Boo extends Foo { }
// Woofy.java
package org.domain
// line n1
public class Woofy implements Boo, Foo { }
// Test.java
package.org;
public class Test {
public static void main(String[] args) {
Foo obj=new Woofy();
Which set modifications enable the code to compile and run?
A. At line n1, Insert: import facades;At line n2, insert:import facades;import org.domain;
B. At line n1, Insert: import facades.*;At line n2, insert:import facades;import org.*;
C. At line n1, Insert: import facades.*;At line n2, insert:import facades.Boo;import org.*;
D. At line n1, Insert: import facades.Foo, Boo;At line n2, insert:import org.domain.Woofy;
E. At line n1, Insert: import facades.*;At line n2, insert:import facades;import org.domain.Woofy;
Answer: E
Q5. Which of the following can fill in the blank in this code to make it compile?
A. abstract
B. final
C. private
D. default
E. int
Answer: C
Explanation:
From Java SE 8, we can use static and/or default methods in interfaces, but they should be non abstract methods. SO in this case using default in blank is completely legal. Hence option C is correct. Option A is incorrect as given method is not abstract, so can't use abstract there. Options B and E are incorrect as we can't have non abstract method interface if they are not default or static. httpsy/docs.oracle.com/javase/tutorial/iava/landl/defaultmethods.html
Q6. Given:
What is the result?
A. 2 4 6 8 10 12
B. 2 4 6 8 10 12 14
C. Compilation fails
D. The program prints multiple of 2 infinite times
E. The program prints nothing
Answer: B
Q7. Given:
public class String1 {
public static void main(String[] args) {
String s = "123";
if (s.length() >2)
s.concat("456");
for(int x = 0; x <3; x++)
s += "x";
System.out.println(s);
}
}
What is the result?
A. 123
B. 123xxx
C. 123456
D. 123456xxx
E. Compilation fails
Answer: B
Explanation: 123xxx
The if clause is not applied.
Note: Syntax of if-statement:
if ( Statement ) {
}
Q8. 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
Q9. Given:
public class Test {
public static void main(String[] args) {
try {
String[] arr =new String[4];
arr[1] = "Unix";
arr[2] = "Linux";
arr[3] = "Solarios";
for (String var : arr) {
System.out.print(var + " ");
}
} catch(Exception e) {
System.out.print (e.getClass());
}
}
}
What is the result?
A. Unix Linux Solaris
B. Null Unix Linux Solaris
C. Class java.lang.Exception
D. Class java.lang.NullPointerException
Answer: B
Explanation: null Unix Linux Solarios
The first element, arr[0], has not been defined.
Q10. 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