Output of Java Programs | Set 51 (Regular Inner class)
Last Updated :
29 Sep, 2022
Prerequisite : Inner Classes in Java
1. What will be the .class file name of the Inner class?
JAVA
class Outer {
class Inner {
public
void m1()
{
System.out.println("Hello Geeks");
}
}
}
Options: 1.Outer.Inner.class 2.Inner.class 3.Outer.class 4.Outer$Inner.class Output:
The answer is option (4)
Explanation : The java compiler creates two class files in case of inner class. The class file name of inner class is "Outer$Inner". $ symbol is used to represent inner classes.
2. What will be the output of the following program if we use java Outer$Inner in command prompt?
JAVA
class Outer {
class Inner {
public
void m1()
{
System.out.println("Hii");
}
} public static void main(String[] args)
{
Outer o = new Outer();
o.m1();
}
}
Options: 1.Hii 2.Compile time error 3.Run time error 4.No Output:
The answer is option (3)
Explanation : As we know that compiler is not responsible to check whether class contains main() method or not. But during the time of execution JVM check whether class contains main() method or not. If the class does not contain main() method then we will get Run time error saying NoSuchMethodError:main.
3. What will be the output of the following program?
JAVA
class Outer {
class Inner {
public
static void main()
{
System.out.println("Hii");
}
} Outer o = new Outer();
o.main();
}
Options: 1.Hii 2.Run time 3.Compile time error 4.No Output :
The answer is option (3)
Explanation : In the above program, we will get compile time error saying "Inner classes can't have static declaration" and here in the program we declare a static method.
4.What will be the option is suitable to replace with Line-1 to access m1() method of the Inner class?
JAVA
class Outer {
class Inner {
public
void m1()
{
System.out.println("Hii");
}
} public static void main(String[] args)
{
Line - 1
}
}
Options: 1.Outer o=new Outer(); Outer.Inner i=o.new Inner(); i.m1(); 2.Outer.Inner i=new Outer().new Inner(); i.m1(); 3.new Outer().new Inner().m1(); 4.None Output:
The answer is option (1), (2), (3)
Explanation : In the concept of Inner classes, we first have to create object of Outer class then with the help of Outer class Object we can create Object of Inner class. With the help of Inner class Object we can access instance method of Inner class.
5.Which is true about a Normal/Regular inner class? 1.It must be marked final. 2.It can be marked native. 3.It must be marked public. 4.It can be marked static. Output:
The answer is option (4)
Explanation : The applicable modifiers for Regular/Normal Inner classes are public, final, default, strictfp, abstract, private, protected, static. But it is not mandatory to use.
Similar Reads
Output of Java Programs | Set 52 (Strings Class) Prerequisite : Basics of Strings class in java 1. What is the Output Of the following Program Java class demo1 { public static void main(String args[]) { String str1 = "java"; char arr[] = { 'j', 'a', 'v', 'a', ' ', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' }; String str2 = new
5 min read
Output of Java Programs | Set 37 (If-else) Prerequisite: if else, for loops 1. What will be the output for the following program? JAVA public class Test { public static void main(String[] args) { for (;;) System.out.println("GEEKS"); } } Options: 1.GEEKS 2.Compile time error 3.Run time Exception 4.GEEKS (Infinitely) The answer is o
3 min read
Output of Java Programs | Set 33 (Collections) Prerequisite: Java - Collections 1. What is the output of following Java Program? Java import java.util.ArrayList; class Demo { public void show() { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(4); list.add(7); list.add(1); for (int number : list) { System.out.print(numbe
3 min read
Output of Java Programs | Set 50 Q 1. What is the output of this program? Java class Test { public final int a; } class Example { public static void main(String args[]) { Test obj = new Test(); System.out.println(obj.a); } } Option A. 0 B. Garbage value C. Compile time error : variable is not initialized D. Run time error : a is th
3 min read
Output of Java Program | Set 1 Difficulty Level: Rookie Predict the output of the following Java Programs.Program 1: Java // filename Main.java class Test { protected int x, y; } class Main { public static void main(String args[]) { Test t = new Test(); System.out.println(t.x + " " + t.y); } } Output: 0 0 In Java, a pro
3 min read
Output of Java Programs | Set 31 Prerequisite : Arrays in Java 1. What will be the output of the following program? Java public class Test { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; System.out.println(arr); } } Options: 1. 1 2. Compile-time error 3. 1 2 3 4 5 4. [I@Hashcode_in_Hexadecimal Output: The
3 min read
Output of Java Programs | Set 38 (Arrays) Prerequisite : Arrays in Java Question 1. What is the output of this question JAVA class Test1 { public static void main(String[] args) { int arr[] = { 11, 22, 33 }; System.out.print(arr[-2]); } } Option A) 11 33 B) Error C) exception D) 11 -33 Output: C Explanation : We will get java.lang.ArrayInde
3 min read
Output of Java Programs | Set 53 (String Comparison) Prerequisite : String Comparison in Java 1. What should be the output of this program? Java class GeeksforGeeks { public static void main(String args[]) { String GfG1 = "Welcome to GeeksforGeeks"; boolean GfG2; GfG2 = GfG1.startsWith("hello"); System.out.println(GfG2); } } a) tru
3 min read
Output of Java Program | Set 11 Predict the output of following Java programs: Question 1 : Java public class Base { private int data; public Base() { data = 5; } public int getData() { return this.data; } } class Derived extends Base { private int data; public Derived() { data = 6; } private int getData() { return data; } public
4 min read
Output of Java Programs | Set 14 (Constructors) Prerequisite - Java Constructors 1) What is the output of the following program? Java class Helper { private int data; private Helper() { data = 5; } } public class Test { public static void main(String[] args) { Helper help = new Helper(); System.out.println(help.data); } } a) Compilation error b)
3 min read