Object Oriented Programming using Java Questions and Answers – Final Class

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

1. What is the output of the following Java code?

final class Final
{
    static String s = "Sanfoundry";
}
public class Program extends Final
{
    public static void main(String[] args)
    {
        System.out.println(s);
    }
}

a) Garbage Value
b) Sanfoundry
c) Compilation Error
d) Runtime Error
View Answer

Answer: c
Explanation: Since the class Final has been declared as final, it cannot be inherited by any other class. The ‘final’ keyword indicates the same in the above program. This is the main objective of using the ‘final’ keyword. Since ‘s’ is a static variable, it can be called inside the main() method which is also a static class.
Output:

$ javac Program.java
$ java Program
error: cannot inherit from final Final

2. What is the output of the following Java code?

advertisement
advertisement
final class Final extends Program
{
    public void Print()
    {
        System.out.println(s);
    }
}
class Program
{
    String s = "Sanfoundry";
}
public class FinalCall 
{
    public static void main(String[] args)
    {
        Final f = new Final();
        f.Print();
    }
}

a) Garbage Value
b) Sanfoundry
c) Runtime Error
d) Compilation Error
View Answer

Answer: b
Explanation: If a class is final, it cannot be inherited or extended by sub classes. Final classes cannot be inherited by any other classes despite their accessibility, but they can inherit other classes. Therefore Final inherits the properties of class Program.
Output:

$ javac FinalCall.java
$ java FinalCall
Sanfoundry

3. What is the output of the following Java code?

advertisement
static final class Program
{
    public void display(char ch)
    {
        System.out.println("The ascii value of "+ch+" is "+(int)ch);
    }
}
public class FinalExe
{
    public static void main(String[] args) 
    {
        Program.display('c');
    }
}

a) The ascii value of c is 67
b) The ascii value of c is 99
c) Runtime Error
d) Compilation Error
View Answer

Answer: d
Explanation: Java doesn’t allow a final class to be given static privileges as it leads to security concerns and contradicts the concept of final classes. Hence it leads to a compilation error as Program is an outer class.
Output:

advertisement
$ javac FinalExe.java
$ java FinalExe
error: modifier static not allowed here
error: non-static method display(char) cannot be referenced from a static context

4. What is the output of the following Java code?

public class Outer
{
    class Program
    {
        String s = "Sanfoundry";
    }
    final class Final extends Program
    {
        void print()
        {
            for(int i=0;i<s.length();i++)
            {
                if(i%2==0)
                System.out.print(s.charAt(i));
            }
        }
    }
    public static void main(String[] args) 
    {
        Outer i = new Outer();
        Outer.Final j = i.new Final();
        j.print();
    }
}

a) afudy
b) Snonr
c) Sanfoundry
d) Compilation Error
View Answer

Answer: b
Explanation: Since class Final extends class Program, it can access the value of String s. The for loop inside the Final class prints only the characters at even indexes starting from 0 which is Snonr.
Output:

$ javac Outer.java
$ java Outer
Snonr

5. What is the output of the following Java code?

public class StaticFinalClass
{
    static final class Final
    {
        Integer i = new Integer(6124);
        void ClassCheck()
        {
            System.out.println(i.getClass());
            System.out.println(i.getClass().getSuperclass());
        }
    }
    public static void main(String[] args) 
    {
        StaticFinalClass.Final i = new StaticFinalClass.Final();
        i.ClassCheck();
    }
}

a) Compilation Error
b)

class java.io.Integer
class java.io.Object

c)

class java.io.Integer
class java.io.Number

d)

class java.lang.Integer
class java.lang.Number
View Answer
Answer: d
Explanation: Integer is a subclass of the main class Number in the java.lang or the java language package. java.lang.Number class is the superclass of the class java.lang.Integer. Final class is a nested class inside the class StaticFinalClass.
Output:

$ javac StaticFinalClass.java
$ java StaticFinalClass
class java.lang.Integer
class java.lang.Number

6. What is the output of the following Java code?

final class Program
{
    public void Loop()
    {
        for(int i=1;i>0;i++);
        {
            System.out.println(i);
        }
    }
}
public class LoopClass
{
    public static void main (String[] args) {
        Program i = new Program();
        i.Loop();
    }
}

a) Infinite loop
b) Compilation Error
c)

1
0

d)

0
1
View Answer
Answer: b
Explanation: In the function Loop, the for loop is followed by a semicolon, hence ending the loop at that point. The code in the curly brackets cannot be executed, hence leads to a compilation error.
Output:

$ javac LoopClass.java
$ java LoopClass 
error: cannot find symbol System.out.println(i);

7. What is the output of the following Java code?

public final class Check
{
    public static void main (String[] args) 
    {
        for(short i=32767;i>0;i++)
        {
            System.out.println(i);
        }
    }
}

a)

32767
32768
32769
32700

b) Infinite loop
c) Compilation Error
d) 32767
View Answer

Answer: d
Explanation: Since the limit of signed short datatype is 32767, upon increasing the value, it changes to the other extreme end, which is -32768 which is less than 0, hence ending the loop.
Output:

$ javac Check.java
$ java Check
32767

8. What is the output of the following Java code?

public final class StringBuffer
{
    public static void main (String[] args) 
    {
        StringBuffer s = new StringBuffer("Sonfoundry");  
        s.setCharAt(2,'a');  
        System.out.println(s);  
    }
}

a) Sanfoundry
b) Saonfoundry
c) Soanfoundry
d) Soafoundry
View Answer

Answer: d
Explanation: setCharAt is a function that changes the character at the specified index position of a StringBuffer to the specified character. Here, index 2 corresponds to the third character in the string, which is ‘n’, and changes it to ‘a’.
Output:

$ javac StringBuffer.java
$ java StringBuffer
Soafoundry

9. What is the output of the following Java code?

public final class StaticString
{
    static String x;
    static int y;
    public static void main (String[] args) 
    {
        StaticString i = new StaticString();
        System.out.println(x+y);
    }
}

a) “”9999999
b) “”0
c) ‘\0’0
d) null0
View Answer

Answer: d
Explanation: When an object of a class is created, the variables of the class are given default values if they are not declared by the user. Integer’s default value is 0 and String’s default value is null according to the JVM.
Output:

$ javac StaticString.java
$ java StaticString
null0

10. What is the output of the following Java code?

public final class Inheritance
{
    class Program
    {
        String s = "Class Program";
    }
    class Print extends Program
    {
        void display()
        {
            System.out.println(s);
        }
    }
    public static void main (String[] args) 
    {
        Inheritance i = new Inheritance();
        Inheritance.B j = i.new B();
        j.display();
    }
}

a) Compilation Error
b) Runtime Error
c) Null
d) Class Program
View Answer

Answer: d
Explanation: final keyword when given to classes makes them uninheritable, but subclasses of a final class can inherit other classes within the final class, hence class B inherits String s from class Program and prints it.
Output:

$ javac Inheritance.java
$ java Inheritance
Class Program

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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.