SCJP : Declarations, Initialization and Scoping Mock Exam Practice Questions
Questions no -1
What is the output for the below code ?
public interface TestInf {
int i =10;
}
public class Test {
public static void main(String... args) {
TestInf.i=12;
System.out.println(TestInf.i);
}
}
options
A)Compile with error
B)10
C)12
D) Runtime Exception
Correct answer is : A
Explanations : All the variables declared in interface is Implicitly static and final , therefore can't change the value.
Questions no -2
What is the output for the below code ?
public class Test {
static { int a = 5; }
public static void main(String[] args){
System.out.println(a);
}
}
options
A)Compile with error
B)5
C)0
D) Runtime Exception
Correct answer is : A
Explanations : A variable declared in a static initialiser is not accessible outside its enclosing block.
Questions no -3
What is the output for the below code ?
class A {
{ System.out.print("b1 "); }
public A() { System.out.print("b2 "); }
}
class B extends A {
static { System.out.print("r1 "); }
public B() { System.out.print("r2 "); }
{ System.out.print("r3 "); }
static { System.out.print("r4 "); }
}
class C extends B {
public static void main(String[] args) {
System.out.print("pre ");
new C();
System.out.println("post ");
}
}
options
A)r1 r4 pre b1 b2 r3 r2 post
B)r1 r4 pre b1 b2 post
C)r1 r4 pre b1 b2 post r3 r2
D)pre r1 r4 b1 b2 r2 r3 post
Correct answer is : A
Explanations : All static blocks execute first then blocks and constructor.
Blocks and constructor executes (super class block then super class constructor, sub class block then sub class constructor).
Sequence for static blocks is super class first then sub class.
Sequence for blocks is super class first then sub class.
Questions no -4
What is the output for the below code ?
public class Test {
public static void main(String... args) throws Exception {
Integer i = 34;
int l = 34;
if(i.equals(l)){
System.out.println(true);
}else{
System.out.println(false);
}
}
}
options
A)true
B)false
C)Compile Error
D) Runtime Exception
Correct answer is : A
Explanations : equals() method for the integer wrappers will only return true if the two primitive types and the two values are equal.
Questions no -5
Which statement is true about outer class?
options
A)outer class can only declare public , abstract and final
B)outer class may be private
C)outer class can't be abstract
D)outer class can be static
Correct answer is : A
Explanations : outer class can only declare public , abstract and final.
Questions no -6
What is the output for the below code ?
static public class Test {
public static void main(String[] args) {
char c = 'a';
switch(c){
case 65:
System.out.println("one");break;
case 'a':
System.out.println("two");break;
case 3:
System.out.println("three");
}
}
}
options
A)one
B)two
C)Compile error - char can't be permitted in switch statement
D)Compile error - Illegal modifier for the class Test; only public, abstract & final are permitted.
Correct answer is : D
Explanations : outer class can only declare public , abstract and final.
Illegal modifier for the class Test; only public, abstract & final are permitted
|