Java-Questions

docx

School

Stevens Institute Of Technology *

*We aren’t endorsed by this school

Course

556

Subject

Computer Science

Date

Feb 20, 2024

Type

docx

Pages

25

Uploaded by ProfSnowWombat32

Report
1) Tinku has written the code like below. But it is showing compile time error. Can you identify what mistake he has done? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 class X { //Class X Members } class Y { //Class Y Members } class Z extends X, Y { //Class Z Members } View Answer 2) What will be the output of this program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 class A { int i = 10 ; } class B extends A { int i = 20 ; } public class MainClass { public static void main(String[] args) { A a = new B(); System.out.println(a.i); } }
1 9 View Answer 3) What will be the output of the below program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 class A { { System.out.println( 1 ); } } class B extends A { { System.out.println( 2 ); } } class C extends B { { System.out.println( 3 ); } } public class MainClass { public static void main(String[] args) { C c = new C(); } }
2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 View Answer 4) Can a class extend itself? View Answer 5) What will be the output of the following program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 class A { String s = "Class A" ; } class B extends A { String s = "Class B" ; { System.out.println( super .s); } } class C extends B { String s = "Class C" ; { System.out.println( super .s); } } public class MainClass { public static void main(String[] args) { C c = new C();
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 System.out.println(c.s); } } View Answer 6) What will be the output of this program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 class A { static { System.out.println( "THIRD" ); } } class B extends A { static { System.out.println( "SECOND" ); } } class C extends B { static { System.out.println( "FIRST" ); }
6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 } public class MainClass { public static void main(String[] args) { C c = new C(); } } View Answer 7) What will be the output of the below program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 class A { public A() { System.out.println( "Class A Constructor" ); } } class B extends A { public B() { System.out.println( "Class B Constructor" ); } } class C extends B {
4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 public C() { System.out.println( "Class C Constructor" ); } } public class MainClass { public static void main(String[] args) { C c = new C(); } } View Answer 8) Private members of a class are inherited to sub class. True or false? View Answer 9) What will be the output of the following program? 1 2 3 4 5 6 7 8 9 1 0 class X { static void staticMethod() { System.out.println( "Class X" ); } } class Y extends X { static void staticMethod()
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 { System.out.println( "Class Y" ); } } public class MainClass { public static void main(String[] args) { Y.staticMethod(); } } View Answer 10) Below code is showing compile time error. Can you suggest the corrections? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 class X { public X( int i) { System.out.println( 1 ); } } class Y extends X { public Y() { System.out.println( 2 ); } } View Answer 11) What is wrong with the below code? Why it is showing compile time error?
1 2 3 4 5 6 7 8 9 1 0 1 1 public class A { public A() { System.out.println( 1 ); super (); System.out.println( 2 ); } } View Answer 12) You know that compiler will keep super() calling statement implicitly as a first statement in every constructor. What happens if we write this() as a first statement in our constructor? View Answer 13) Can you find out the error in the below code? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 public class A { public A( int i) { } } class B extends A { } View Answer 14) Which class is a default super class for every class in java? View Answer 15) Can you find out the error in the below code? 1 2 3 4 5 6 7 8 9 1 0 public class A { public A() { super (); this ( 10 ); } public A( int i) {
1 1 1 2 1 3 1 4 System.out.println(i); } } View Answer 16) What will be the output of this program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 class M { static { System.out.println( 'A' ); } { System.out.println( 'B' ); } public M() { System.out.println( 'C' ); } } class N extends M { static { System.out.println( 'D' ); } { System.out.println( 'E' ); } public N() { System.out.println( 'F' ); } } public class MainClass { public static void main(String[] args) { N n = new N(); } }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 View Answer 17) What will be the output of the below program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 class M { int i; public M( int i) { this .i = i--; } } class N extends M { public N( int i) { super (++i); System.out.println(i);
1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 } } public class MainClass { public static void main(String[] args) { N n = new N( 26 ); } } View Answer 18) What will be the output of the following program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 class M { int i = 51 ; public M( int j) { System.out.println(i); this .i = j * 10 ; } } class N extends M { public N( int j) { super (j); System.out.println(i); this .i = j * 20 ;
1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 } } public class MainClass { public static void main(String[] args) { N n = new N( 26 ); System.out.println(n.i); } } View Answer 19) Why Line 10 in the below code is showing compilation error? 1 2 3 4 5 6 7 8 9 1 0 1 1 class X { private int m = 48 ; } class Y extends X { void methodOfY() { System.out.println(m); } }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
1 2 View Answer 20) What will be the output of the below program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 class X { int m = 1111 ; { m = m++; System.out.println(m); } } class Y extends X { { System.out.println(methodOfY()); } int methodOfY() { return m-- + --m; } } public class MainClass { public static void main(String[] args) { Y y = new Y(); } }
2 9 3 0 View Answer 21) What will be the output of this program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 class A { static String s = "AAA" ; static { s = s + "BBB" ; } { s = "AAABBB" ; } } class B extends A { static { s = s + "BBBAAA" ; } { System.out.println(s); } } public class MainClass { public static void main(String[] args) { B b = new B(); } }
2 8 2 9 3 0 3 1 3 2 3 3 View Answer 22) What will be the output of the following program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 class X { int i = 101010 ; public X() { i = i++ + i-- - i; } static int staticMethod( int i) { return --i; } } class Y extends X { public Y() { System.out.println(staticMethod(i)); } } public class MainClass { public static void main(String[] args) { Y y = new Y(); } }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
2 4 2 5 2 6 2 7 2 8 2 9 3 0 View Answer 23) What happens if both super class and sub class have a field with same name? View Answer 24) Does the below program execute successfully? If yes, what will be the output? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 class A { void A() { System.out.println( 1 ); } } class B extends A { void B() { A(); } } public class MainClass { public static void main(String[] args) { new B().B(); } }
1 2 2 2 3 View Answer 25) How do you prevent a field or a method of any class from inheriting to sub classes? View Answer 26) What will be the output of the below program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 class A { int i = 1212 ; } class B extends A { A a; public B(A a) { this .a = a; } } public class MainClass { public static void main(String[] args) { A a = new A(); B b = new B(a); System.out.println(a.i); System.out.println(b.i); System.out.println(b.a.i); b.a.i = 2121 ; System.out.println( "--------" ); System.out.println(a.i); System.out.println(b.i); } }
2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 View Answer 27) Does java support multiple inheritance? View Answer 28) What will be the output of this program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 class ClassOne { static int i, j = 191919 ; { --i; } { j++; } } public class ClassTwo extends ClassOne { static { i++; }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 static { --j; } public static void main(String[] args) { System.out.println(i); System.out.println(j); } } View Answer 29) Does the fields with default visibility inherited to sub classes outside the package? View Answer 30) Constructors are also inherited to sub classes. True or false? View Answer 31) What will be the output of the below program? 1 2 3 4 5 6 7 8 class A { int [] a = new int [ 5 ]; { a[ 0 ] = 10 ; } }
9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 public class MainClass extends A { { a = new int [ 5 ]; } { System.out.println(a[ 0 ]); } public static void main(String[] args) { MainClass main = new MainClass(); } } View Answer 32) What happens if both super class and sub class have a field with same name? View Answer 33) What will be the output of the below program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 class A { int methodOfA(int i) { i /= 10; return i; } } class B extends A { int methodOfB(int i) { i *= 20;
1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 return methodOfA(i); } } public class MainClass { public static void main(String[] args) { B b = new B(); System.out.println(b.methodOfB(100)); } } View Answer 34) What will be the outcome of the following program? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 class A { static int i; static { i++; } { ++i; } } class B extends A
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 2 9 3 0 3 1 3 2 3 3 { static { --i; } { i--; } } public class MainClass { public static void main(String[] args) { System.out.println( new B().i); } } View Answer 35) Can a class be extended by more than one classes? View Answer 36) Does the below program written correctly? If yes, what will be the output? 1 2 3 4 public class MainClass { public MainClass( int i, int j) {
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 System.out.println(method(i, j)); } int method( int i, int j) { return i++ + ++j; } public static void main(String[] args) { MainClass main = new MainClass( 10 , 20 ); } } View Answer 37) Does the below code prints “Hi….” on the console? If yes, how many times? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 class X { static { Y.methodOfY(); } } class Y extends X { static void methodOfY() { System.out.println( "Hi .... " ); } } public class MainClass { public static void main(String[] args) { Y.methodOfY(); } }
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
9 2 0 2 1 2 2 2 3 View Answer 38) What value the fields ‘i’ and ‘j’ will hold when you instantiate ‘ClassTwo’ in the below code? 1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 class ClassOne { static int i = 111 ; int j = 222 ; { i = i++ - ++j; } } class ClassTwo extends ClassOne { { j = i-- + --j; } } View Answer 39) When you instantiate a sub class, super class constructor will be also executed. True or False? View Answer 40) Does the below code written correctly? If yes, what will be the output? 1 2 3 4 5 6 7 8 9 class One { int x = 2121 ; } class Two { int x = 1212 ;
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 { System.out.println(x); } } public class MainClass { public static void main(String[] args) { Two two = new Two(); } } View Answer Also Read : Classes & Objects Quiz Polymorphism Quiz Abstract Classes Quiz Java Interfaces Quiz Java Modifiers Quiz Nested Classes Quiz Java Enums Quiz i++, i– & ++i, –i Quiz Java Arrays Quiz Java Strings Quiz Java Inheritance Oracle Docs
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help