Q1. Given the code fragment:
Which code fragment prints blue, cyan, ?
A. Option A
B. Option B
C. Option C
D. Option D
Answer: A
Q2. Which statement is true about the default constructor of a top-level class?
A. It can take arguments.
B. It has private access modifier in its declaration.
C. It can be overloaded.
D. The default constructor of a subclass always invokes the no-argument constructor of its superclass.
Answer: D
Explanation: In both Java and C#, a "default constructor" refers to a nullary constructor that is automatically generated by the compiler if no constructors have been defined for the class. The default constructor is also empty, meaning that it does nothing. A programmer-defined constructor that takes no parameters is also called a default constructor.
Q3. Given:
What is the result?
A. Red 0
Orange 0
Green 3
B. Red 0
Orange 0
Green 6
C. Red 0
Orange 1
D. Green 4
E. Compilation fails
Answer: E
Q4. Given:
Given:
public class SuperTest {
public static void main(String[] args) {
statement1
statement2
statement3
}
}
class Shape {
public Shape() {
System.out.println("Shape: constructor");
}
public void foo() {
System.out.println("Shape: foo");
}
}
class Square extends Shape {
public Square() {
super();
}
public Square(String label) {
System.out.println("Square: constructor");
}
public void foo() {
super.foo();
}
public void foo(String label) {
System.out.println("Square: foo");
}
}
}
}
What should statement1, statement2, and statement3, be respectively, in order to produce the result?
Shape: constructor
Square: foo
Shape: foo
A. Square square = new Square ("bar");
square.foo ("bar");
square.foo();
B. Square square = new Square ("bar");
square.foo ("bar");
square.foo ("bar");
C. Square square = new Square ();
square.foo ();
square.foo(bar);
D. Square square = new Square ();
square.foo ();
square.foo("bar");
E. Square square = new Square ();
square.foo ();
square.foo ();
F. Square square = new Square();
square.foo("bar");
square.foo();
Answer: F
Q5. Given:
Which of the following is equivalent to the above code fragment?
A. System.out.printLn(x>10?">,': "<":,'=");
B. System.out.println(x>10? ">"?"<":"=");
C. System.out.println(x>10?">":x<10?"<":"=");
D. System.out.printLn(x>10?">"?,'<"?"=");
E. None of the above
Answer: B
Explanation:
Option A is incorrect as we can't use abstract with non abstract method, (here method has method body.) Option C is incorrect as when overriding method we can't use more restrictive access modifier, so trying to use private to override default access Level method causes a compile time error. Option D is incorrect as default methods (not methods with default access level) are allowed only in interfaces. Option E is incorrect as method all ready has void as return type, so we can't add int there. Option B is correct as we can use final there, since the method is non abstract
https://docs.oracle.com/javase/tutorial/java/landl/polymorphism.html
Q6. boolean log3 = ( 5.0 != 6.0) && ( 4 != 5);
boolean log4 = (4 != 4) || (4 == 4);
System.out.println("log3:"+ log3 + \nlog4" + log4);
What is the result?
A. log3:false log4:true
B. log3:true log4:true
C. log3:true log4:false
D. log3:false log4:false
Answer: B
Q7. Given the code fragments:
Which code fragment, when inserted at line ni, enables the code to print Hank?
A. checkAge (iList, ( ) -> p. get Age ( ) > 40);
B. checkAge(iList, Person p -> p.getAge( ) > 40);
C. checkAge (iList, p -> p.getAge ( ) > 40);
D. checkAge(iList, (Person p) -> { p.getAge() > 40; });
Answer: C
Q8. Which two are benefits of polymorphism?
A. Faster code at runtime
B. More efficient code at runtime
C. More dynamic code at runtime
D. More flexible and reusable code
E. Code that is protected from extension by other classes
Answer: C,D
Q9. Given:
Which constructor initializes the variable x3?
A. Only the default constructor of class X
B. Only the no-argument constructor of class Y
C. Only the no-argument constructor of class Z
D. Only the default constructor of object class
Answer: C
Q10. Given the code fragment:
What is the result?
A. true true
B. true false
C. false false
D. false true
Answer: C