Explore topic-wise MCQs in Java Programming.

This section includes 12 Mcqs, each offering curated multiple-choice questions to sharpen your Java Programming knowledge and support exam preparation. Choose a topic below to get started.

1.

which statement, inserted at line 10, creates an instance of ?

A. Foo.Bar b = new Foo.Bar();
B. Foo.Bar b = f.new Bar();
C. Bar b = new f.Bar();
D. Bar b = f.new Bar();
Answer» C. Bar b = new f.Bar();
2.

which statement, if placed in a class other than or , instantiates an instance of the nested class?

A. MyOuter.MyInner m = new MyOuter.MyInner();
B. MyOuter.MyInner mi = new MyInner();
C. MyInner mi = new MyOuter.MyInner();
Answer» B. MyOuter.MyInner mi = new MyInner();
3.

which one create an anonymous inner class from within class Bar?

A. Boo f = new Boo(24) { };
B. Boo f = new Bar() { };
C. Bar f = new Boo(String s) { };
D. Boo f = new Boo.Bar(String s) { };
Answer» C. Bar f = new Boo(String s) { };
4.

What is java_g used for?

A. Executing a class with optimization turned off
B. Using the jdb tool
C. None of the above
D. To provide information about deprecated methods
Answer» B. Using the jdb tool
5.

public class MyOuter { public static class MyInner { public static void foo() { } } } which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?

A. MyOuter.MyInner m = new MyOuter.MyInner();
B. MyOuter.MyInner mi = new MyInner();
C. MyOuter m = new MyOuter();MyOuter.MyInner mi = m.new MyOuter.MyInner();
D. MyInner mi = new MyOuter.MyInner();
Answer» B. MyOuter.MyInner mi = new MyInner();
6.

What will be the output of the program? public class HorseTest { public static void main (String [] args) { class Horse { public String name; /* Line 7 */ public Horse(String s) { name = s; } } /* class Horse ends */ Object obj = new Horse("Zippo"); /* Line 13 */ Horse h = (Horse) obj; /* Line 14 */ System.out.println(h.name); } } /* class HorseTest ends */

A. An exception occurs at runtime at line 10.
B. It prints "Zippo".
C. Compilation fails because of an error on line 7.
D. Compilation fails because of an error on line 13.
Answer» C. Compilation fails because of an error on line 7.
7.

What will be the output of the program? public class TestObj { public static void main (String [] args) { Object o = new Object() /* Line 5 */ { public boolean equals(Object obj) { return true; } } /* Line 11 */ System.out.println(o.equals("Fred")); } }

A. It prints "true".
B. It prints "Fred".
C. An exception occurs at runtime.
D. Compilation fails
Answer» E.
8.

What will be the output of the program? public abstract class AbstractTest { public int getNum() { return 45; } public abstract class Bar { public int getNum() { return 38; } } public static void main (String [] args) { AbstractTest t = new AbstractTest() { public int getNum() { return 22; } }; AbstractTest.Bar f = t.new Bar() { public int getNum() { return 57; } }; System.out.println(f.getNum() + " " + t.getNum()); } }

A. 57 22
B. 45 38
C. 45 57
D. An exception occurs at runtime.
Answer» B. 45 38
9.

class Foo { class Bar{ } } class Test { public static void main (String [] args) { Foo f = new Foo(); /* Line 10: Missing statement ? */ } } which statement, inserted at line 10, creates an instance of Bar?

A. Foo.Bar b = new Foo.Bar();
B. Foo.Bar b = f.new Bar();
C. Bar b = new f.Bar();
D. Bar b = f.new Bar();
Answer» C. Bar b = new f.Bar();
10.

class Boo { Boo(String s) { } Boo() { } } class Bar extends Boo { Bar() { } Bar(String s) {super(s);} void zoo() { // insert code here } } which one create an anonymous inner class from within class Bar?

A. Boo f = new Boo(24) { };
B. Boo f = new Bar() { };
C. Bar f = new Boo(String s) { };
D. Boo f = new Boo.Bar(String s) { };
Answer» C. Bar f = new Boo(String s) { };
11.

What will be the output of the program? public class Foo { Foo() { System.out.print("foo"); } class Bar { Bar() { System.out.print("bar"); } public void go() { System.out.print("hi"); } } /* class Bar ends */ public static void main (String [] args) { Foo f = new Foo(); f.makeBar(); } void makeBar() { (new Bar() {}).go(); } }/* class Foo ends */

A. Compilation fails.
B. An error occurs at runtime.
C. It prints "foobarhi"
D. It prints "barhi"
Answer» D. It prints "barhi"
12.

Which is true about an anonymous inner class?

A. It can extend exactly one class and implement exactly one interface.
B. It can extend exactly one class and can implement multiple interfaces.
C. It can extend exactly one class or implement exactly one interface.
D. It can implement multiple interfaces regardless of whether it also extends a class.
Answer» D. It can implement multiple interfaces regardless of whether it also extends a class.