Sail E0 Webinar

MCQs

Total Questions : 30 | Page 2 of 3 pages
Question 11. What will be the output?
class One{
final int a = 15;
}
class Two extends One{
final int a = 20;
}
public class Test extends Two{
final int a = 30;
public static void main(String args[]){
Test t = new One();
System.out.print(t.a);
}
}
  1.    15
  2.    20
  3.    30
  4.    Compiler Error
  5.    None of these
 Discuss Question
Answer: Option D. -> Compiler Error
Question 12. What will be the result after compiling this code?
class SuperClass{
public int doIt(String str, Integer... data)throws Exception{
String signature = "(String, Integer[])";
System.out.println(str + " " + signature);
return 1;
}
}
public class Test extends SuperClass{
public int doIt(String str, Integer... data){
String signature = "(String, Integer[])";
System.out.println("Overridden: " + str + " " +signature);
return 0;
}
public static void main(String... args){
SuperClass sb = new Test();
sb.doIt("hello", 3);
}
}
  1.    Overridden: hello (String, Integer[])
  2.    hello (String, Integer[])
  3.    Compilation fails
  4.    None of these
 Discuss Question
Answer: Option C. -> Compilation fails
Question 13. What will be the output?
class A{
int i = 10;
public void printValue(){
System.out.print("Value-A");
}
}
class B extends A{
int i = 12;
public void printValue(){
System.out.print("Value-B");
}
}
public class Test{
public static void main(String args[]){
A a = new B();
a.printValue();
System.out.print(a.i);
}
}
  1.    Value-B 11
  2.    Value-B 10
  3.    Value-A 10
  4.    Value-A 11
  5.    None of these
 Discuss Question
Answer: Option B. -> Value-B 10
Question 14. Class A{
A(String s){}
A(){}
}
1. class B extends A{
2. B(){}
3. B(String s){
4. super(s);
5. }
6. void test(){
7. // insert code here
8. }
9. }
Which of the below code can be insert at line 7 to make clean compilation ?
  1.    A a = new B();
  2.    A a = new B(5);
  3.    A a = new A(String s);
  4.    All of the above
  5.    None of these
 Discuss Question
Answer: Option A. -> A a = new B();
Question 15. Determine output:
class A{
public void printValue(){
System.out.println("Value-A");
}
}
class B extends A{
public void printNameB(){
System.out.println("Name-B");
}
}
class C extends A{
public void printNameC(){
System.out.println("Name-C");
}
}
1. public class Test{
2. public static void main (String[] args){
3. B b = new B();
4. C c = new C();
5. newPrint(b);
6. newPrint(c);
7. }
8. public static void newPrint(A a){
9. a.printValue();
10. }
11. }
  1.    Value-A Name-B
  2.    Value-A Value-A
  3.    Value-A Name-C
  4.    Name-B Name-C
  5.    None of these
 Discuss Question
Answer: Option B. -> Value-A Value-A
Question 16. Determine output:
class A{
public void printName(){
System.out.println("Name-A");
}
}
class B extends A{
public void printName(){
System.out.println("Name-B");
}
}
class C extends A{
public void printName(){
System.out.println("Name-C");
}
}
1. public class Test{
2. public static void main (String[] args){
3. B b = new B();
4. C c = new C();
5. b = c;
6. newPrint(b);
7. }
8. public static void newPrint(A a){
9. a.printName();
10. }
11. }
  1.    Name B
  2.    Name C
  3.    Compilation fails due to an error on lines 5
  4.    Compilation fails due to an error on lines 9
  5.    None of these
 Discuss Question
Answer: Option C. -> Compilation fails due to an error on lines 5
Question 17. What will be the result of compiling and running the given code?
class A{
int b=10;
private A(){
this.b=7;
}
int f(){
return b;
}
}
class B extends A{
int b;
}
public class Test{
public static void main(String[] args){
A a = new B();
System.out.println(a.f());
}
}
  1.    Compilation Fails
  2.    Prints 0
  3.    Prints 10
  4.    Prints 7
  5.    None of these
 Discuss Question
Answer: Option A. -> Compilation Fails
Question 18. What is the output for the below code ?
class A{
private void printName(){
System.out.println("Value-A");
}
}
class B extends A{
public void printName(){
System.out.println("Name-B");
}
}
public class Test{
public static void main (String[] args){
B b = new B();
b.printName();
}
}
  1.    Value-A
  2.    Name-B
  3.    Value-A Name-B
  4.    Compilation fails - private methods can't be override
  5.    None of these
 Discuss Question
Answer: Option B. -> Name-B
Question 19. What will be the result of compiling and executing the following program code?
class Vehicle{
public void printSound(){
System.out.print("vehicle");
}
}
class Car extends Vehicle{
public void printSound(){
System.out.print("car");
}
}
class Bike extends Vehicle{
public void printSound(){
System.out.print("bike");
}
}
public class Test{
public static void main(String[] args){
Vehicle v = new Car();
Bike b = (Bike) v;
v.printSound();
b.printSound();
}
}
  1.    Compilation fails.
  2.    ClassCastException exception is thrown at runtime.
  3.    "vehiclecar" is printed.
  4.    "vehiclebike" is printed.
  5.    "carcar" is printed.
 Discuss Question
Answer: Option B. -> ClassCastException exception is thrown at runtime.
Question 20. Determine output:
class Small{
public Small(){
System.out.print("a ");
}
}
class Small2 extends Small{
public Small2(){
System.out.print("b ");
}
}
class Small3 extends Small2{
public Small3(){
System.out.print("c ");
}
}
public class Test{
public static void main(String args[]){
new Small3();
}
}
  1.    a
  2.    c
  3.    a b c
  4.    c b a
  5.    The code runs without output..
 Discuss Question
Answer: Option C. -> a b c

Latest Videos

Latest Test Papers