Object Oriented Programming using Java Questions and Answers – Static Class

This set of Object Oriented Programming (OOPs) using Java Multiple Choice Questions & Answers (MCQs) focuses on “Static Class”.

1. What will be the output of the following Java code?

public class Sum
{
    public static class Inner
    {
        static int sum=0;
        public static void add(int a, int b)
        {
            sum=a+b;
        }
        public void print()
        {
            System.out.println(sum);
        }
    }
    public static void main(String[] args) 
    {
        Sum.Inner.add(10,20);
        Sum.Inner i = new Sum.Inner();
        i.print();
    }
}

a) Runtime Error
b) Compilation Error
c) 0
d) 30
View Answer

Answer: d
Explanation: For calling a static function of a static inner class, we do not require an object of the main class hence, when add is called from main function, 30 is stored in sum.
Output:

$ javac Sum.java
$ java Sum
30

2. What will be the output of the following Java code?

advertisement
advertisement
public class getClass
{
    public static class Inner
    {
        public void print()
        {
            System.out.println("Inner");
        }
    }
    public static void main(String[] args) 
    {
        getClass.Inner i = new getClass.Inner();
        System.out.println(i.getClass());
        i.print();
    }
}

a)

class getClass$Inner 
Inner

b) class getClass.InnerInner
c)

advertisement
class getClass.Inner
Inner

d)

class Inner
Inner
View Answer
Answer: a
Explanation: The getClass function returns the name of the class of the object it is called with. An inner static class’ name is given by the outer class’ name followed by the ‘$’ symbol and then the name of the inner class.
Output:

advertisement
$ javac getClass.java
$ java getClass
class getClass$Inner 
Inner

3. What will be the output of the following Java code?

public class StaticOuter
{
    public static class StaticInner
    {
        public void display()
        {
            System.out.println("StaticInner");
        }
    }
    public class Inner
    {
        public void display()
        {
            System.out.println("Inner");
        }
    }
    public static void main(String[] args) 
    {
        StaticOuter.StaticInner i = new StaticOuter.StaticInner();
        System.out.println(i.getClass());
        i.display();        
        StaticOuter.Inner j = new StaticOuter.Inner();
        System.out.println(j.getClass());
        j.display();
    }
}

a)

class StaticOuter$StaticInner 
StaticInner
class StaticOuter$Inner 
Inner

b) Runtime Error
c)

class StaticOuter.StaticInner 
StaticInner
class StaticOuter.Inner 
Inner

d) Compilation Error
View Answer

Answer: d
Explanation: As the inner class is non-static, it is not possible to directly create an object for it, instead we have to first create an object for the outer class. Therefore an error is thrown by the compiler as an object of inner class cannot be created in this case.
Output:

$ javac StaticOuter.java
$ java StaticOuter
error: non-static variable this cannot be referenced from a static context

4. What will be the output of the following Java code?

public class Program
{
    public static class Char
    {
        public void print(int a)
        {
            System.out.println((char)c);
        }
    }
    public static void main(String[] args)     
    {
        Program.Inner i = new Program.Inner();
        i.print(97);
    }
}

a) A
b) The unicode character of the integer is A
c) The unicode character of the integer is a
d) Error
View Answer

Answer: c
Explanation: Static members belong to the class instead of a specific instance and since it is a static class, we can directly make an object for the class. The unicode equivalent of 97 is ‘a’ and therefore it is printed.
Output:

$ javac Program.java
$ java Program
a

5. What will be the output of the following Java code?

public class DisplayProg
{
    public static class Display
    {
        String a;
        Display(String a)
        {
            this.a = a;
        }
        public static void display()
        {
            System.out.println(a);
        }
    }
    public static void main(String[] args) 
    {
        DisplayProg.Display i = new DisplayProg.Display("A","A");
        i.display();
    }
}

a) Strings are equal
b) The strings aren’t equal
c) Runtime Error
d) Compilation Error
View Answer

Answer: d
Explanation: As display is a static function, the string variable ‘a’ has to be static too. Therefore, it leads to a compilation error in the compare funtion while accessing the string variable ‘a’.
Output:

$ javac DisplayProg.java
$ java DisplayProg
error: non-static variable a cannot be referenced from a static context

6. What will be the output of the following Java code?

public class String
{
    public static class obj
    {
        String a;
        int b;
        obj(String x, int y)
        {
            a=x;
            b=y;
        }
    }
    public static void main(String[] args) 
    {
        String.obj i = new String.obj("San",10);
        System.out.println(i);
        System.out.println(i.toString());
    }
}

a)

San 10
"San 10" 

b)

"San" 10
""San" 10" 

c)

String$obj@2a139a55
"San" 10 

d)

String$obj@2a139a55
String$obj@2a139a55
View Answer
Answer: d
Explanation: When we try to print an object directly, it returns the name of the class, followed by ‘@’ and a hashcode in hexadecimal whereas the toString() method converts it into a string.
Output:

$ javac String.java
$ java String
String$obj@2a139a55 
String$obj@2a139a55

7. What will be the output of the following Java code?

public class StringProgram
{
    public static class Compare
    {
        static String a,b;
        Compare(String x, String y)
        {
            a=x;
            b=y;
        }
        public static void compare()
        {
            if(a==b)
                System.out.println("The strings are equal");
            else
                System.out.println("The strings aren't equal");
        }
    }
    public static void main(String[] args) 
    {
        String a = new String("Sanfoundry");
        String b = new String("Sanfoundry");
        StringProgram.Compare i = new StringProgram.Compare(a, b);
        i.compare();
    }
}

a) Runtime Error
b) Compilation Error
c) The strings are equal
d) The strings aren’t equal
View Answer

Answer: d
Explanation: Strings when created as objects refer to different memory locations. When using ‘==’ operator, they do not correspond to the same location and hence moves to the else part.
Output:

$ javac StringProgram.java
$ java StringProgram
The strings aren't equal

8. What will be the output of the following Java code?

public class charAtFunction
{
    public static class Inner
    {
        public static void indexChar(String a, int b)
        {
            System.out.println(a.charAt(b));
        }
    }
    public static void main(String[] args) 
    {
        charAtFunction.Inner.indexChar("Sanfoundry", 8);
    }
}

a) d
b) r
c) Compilation error
d) Runtime Error
View Answer

Answer: b
Explanation: charAt returns the character at the given index. Since indexing starts at 0, the character at the n+1th position is returned. And since indexChar is a static function, it can be called directly without making an object.
Output:

$ javac charAtFunction.java
$ java charAtFunction
r

9. What will be the output of the following Java code?

public class indexOfFunction
{
    public static class Inner
    {
        public void charPos(String a, char b)
        {
            System.out.println(a.indexOf(b));
            System.out.println(a.indexOf(b,3);
        }
    }
    public static void main(String[] args) 
    {
        indexOfFunction.Inner i = new indexOfFunction.Inner();
        i.charPos("Sanfoundry", 'n');
    }
}

a)

3
7 

b)

3
3 

c)

2
6 

d)

2
2 
View Answer
Answer: c
Explanation: indexOf returns the first index of the given character in the string. When followed by an integer, it starts the search from the specified index of the string. Since ‘n’ is at index 2, it searches for the next instance of ‘n’ in the string.
Output:

$ javac indexOfFunction.java
$ java indexOfFunction
2
6

10. What will be the output of the following Java code?

public class Nested
{
    public static class Push
    {
        public void push(int a, int b)
        {
            System.out.println(a<<b);
        }
    }
    public static void main(String[] args) 
    {
        Nested.Push i = new Nested.Push();
        i.push(10, 2);
    }
}

a) 2.5
b) 2
c) 40
d) 20
View Answer

Answer: c
Explanation: The left shift operator takes two numbers, left shifts the bits of the first operand and the second operand decides the number of places to shift. Here 10, which is 1010 after the push becomes 101000 which is 40.
Output:

$ javac Nested.java
$ java Nested
40

Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).

To practice all areas of Object Oriented Programming (OOPs) using Java, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.