Java Interview Questions On Classes, Objects and Access Specifiers |
23. Which of the following declarations are valid?
[a] private String s = "";
[b] transient double d = 5.0d;
[c] protected int t = 35;
[d] static { System.out.print("Static execution"); }
Answer
[a] private String s = "";
[b] transient double d = 5.0d;
[c] protected int t = 35;
[d] static { System.out.print("Static execution"); }
|
24. The synchronized keyword is used in which of the following?
[a] Class declarations.
[b] Method declarations.
[c] Block of code declarations
[d] Variable declarations.
Answer
[b] Method declarations.
[c] Block of code declarations |
25. What will be printed when you execute the following code?
class A {
A() {
System.out.println("Class A Constructor");
}
}
public class B extends A {
B() {
System.out.println("Class B Constructor");
}
public static void main(String args[]) {
B b = new B();
}
}
[a] Class A Constructor followed by Class B Constructor
[b] Class B Constructor followed by Class A Constructor
[c] Compile time error
[d] Run time error
Answer
[a] Class A Constructor followed by Class B Constructor |
26. Given the piece of code, select the correct answer to replace at the comment line //xxxxx ?
class A {
A(int i) { }
public class B extends A {
B() {
// xxxxx
}
public static void main(String args[]) {
B b = new B();
}
}
[a] super(100);
[b] this(100);
[c] super();
[d] this();
Answer
[a] super(100); |