1)
class A {A() throws Exception {}} // 1
class B extends A {B() throws Exception {}} // 2
class C extends A {} // 3
Which of the following statements is true?
A. Compile-time error at line 1
B. Compile-time error at line 2
C. Compile-time error at line 3
D. None of the above
Answer: C
Default constructor cannot handle exception type Exception thrown by implicit super constructor. Must define an explicit constructor for the class C.In the question compiler will include a default constructor for the class C C(){ } ,without any throws clause.It will generate a compile time error.You should explicitly add a constructor with throws clause to handle exception thrown by constructor of super class A .
2)
abstract class A {} // 1
transient class G {} // 2
private class C {} // 3
static class E {} // 4
Suppose these are top-level class declarations and not nested class declarations.
Which of these declarations would not produce a compile-time error?
A. Line 1
B. Line 2
C. Line 3
D. Line 4
Answer: A
The modifiers, private and static, can be applied to a nested class, but can not be applied to a class that is not nested. A class that is not nested can have public or package access, but not private. The transient
modifier can not be applied to any class; because it is a field modifier.
3)
public class Test {
Test(){
System.out.print(" default ");
}
Test(String s){
this();
System.out.println(s);
}
public static void main(String[] args) {
new Test("java");
}
}
The output of the above code is
A. default java
B. java default
C. java
D. compile-time error
Answer: A
this() is used to call default constructor from another constructor.You can also use as 'this(parms_list);' to call the parameterized constructors.
4)
public class Test {
static final int a;
public static void main(String[] args) {
System.out.println(a);
}
}
//What is the result of attempting to compile and run the program?
A. Prints 0
B. Prints garbage value
C. Prints null
D. Compile-time error
Answer: D
non static final variables should be initialized either at compile-time(ie final int a=10;) or runtime (ie
within each constructor declared).static final variable should be initialized either at compile-time(ie
static final int a=10;) or at runtime(within static blocks).Local final variable should be initialised before
use.
5)
public class Test {
final int a;
Test(){ }
Test(int a){
this.a=a;
}
public static void main(String[] args) {
Test t=new Test(10);
System.out.println(t.a);
}
}
//What is the result of attempting to compile and run the program?
A. Prints 10
B. Prints 0
C. compile-time error
D. run-time error
Answer: C
The final variables should be initialised in all the constructors declared.
6)
public class Test {
Test(){
System.out.print(" default ");
}
Test(String s){
System.out.println(s);
this();
}
public static void main(String[] args) {
new Test("java");
}
}
//What is the result of attempting to compile and run the program?
A. default java
B. java default
C. java
D. Compile-time error
Answer: D
Constructor call must be the first statement in a constructor.ie this() and super() should come on the first
line of a constructor. The 'this()' is used to call another constructor within the same class.'super()' is used
to call constructor on the base class.
7)
class Super{
Super(){
System.out.print(" Super ");}
}
public class Test extends Super {
Test(){
System.out.print(" Sub ");
}
public static void main(String[] args) {
new Test();
}
}
//What is the result of attempting to compile and run the program?
A. Super Sub
B. Sub Super
C. Sub
D. Compile-time error
Answer: A
Within an inherited class hierarchy constructors will be invoked in the order from base class to sub class.Here constructor of class 'Super' execute first then the constructor of the subclass Test invokes.
8)
class Super{
Super(String msg){
System.out.print(" Super"+msg);}
}
public class Test extends Super {
Test(){
System.out.print(" Sub ");
}
public static void main(String[] args) {
new Test();
}
}
//What is the result of attempting to compile and run the program?
A. Super Sub
B. Sub Super
C. Sub
D. Compile-time error
Answer: D
During inheritance the sub class constructor will add a default super() method in it to call the default
constructor on the base class.Here in the base class user added a custom parameterised constructor.Once you add a custom constructor system won't add a default one.Here to solve problem either you add a non parameter constructor in base class or add 'super(String_Value)' in sub class constructor's first line.
9)
public class Test {
public static void main(String[] args) {
final int a;
System.out.println(a);
}
}
//What is the result of attempting to compile and run the program?
A, Prints 0
B. Prints Garbage value
C. Prints null
D. compile-time error
Answer: D
non static final variables should be initialized either at compile-time(ie final int a=10;) or runtime (ie
within each constructor declared).static final variable should be initialized either at compile-time(ie
static final int a=10;) or at runtime(within static blocks).Local final variable should be initialised before
use.
10)
What will be the output of the program?
class A
{
final public int methodl(int a, int b) { return 0; }
}
class B extends A
{
public int method1 (int a, int b) {return 1; }
}
public class Test
{
public static void main(String args[])
{
B b;
System.out.println("x = " + b.method1(0, 1));
}
}
A. x=0
B. x=1
C. compilation-error
D. runtime-error
Answer: C
The code doesn't compile because the method in class A is final and so cannot be overridden.
No comments:
Post a Comment