Hardest Java Exam Trivia Quiz!

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Marikannan
M
Marikannan
Community Contributor
Quizzes Created: 2 | Total Attempts: 5,949
Questions: 20 | Attempts: 5,804

SettingsSettingsSettings
Hardest Java Exam Trivia Quiz! - Quiz

Are you a real programmer? Get ready to take the hardest Java exam trivia quiz that we have brought here for you. The quiz is on Java, and it is going to be very tough. If you know Java very well, only then will you be able to pass this test with a good score? Do you have the confidence? Let's see how well you perform in this amazing quiz.


Questions and Answers
  • 1. 

    Consider the following code and choose the correct option: class X {      int x;      X(int x){         x=2;     } } class Y extends X{      Y(){}      void displayX(){         System.out.print(x);     }     public static void main(String args[]){         new Y().displayX();     } }

    • A.

      Compiles and display 2

    • B.

      Compiles and runs without any output

    • C.

      Compiles and display 0

    • D.

      Compilation error

    Correct Answer
    D. Compilation error
    Explanation
    The code will result in a compilation error because the variable "x" in the constructor of class X is not properly assigned. The statement "x=2;" should be changed to "this.x=2;" to correctly assign the value to the instance variable "x". Without this change, the variable "x" remains uninitialized and will have a default value of 0, which will be displayed in the displayX() method.

    Rate this question:

  • 2. 

    Consider the following code and choose the correct option: class Test{      private void display(){         System.out.println("Display()");}     private static void show() {          display();         System.out.println("show()");     }     public static void main(String arg[]){         show();     } }

    • A.

      Compiles and prints show()

    • B.

      Compiles and prints Display() show()

    • C.

      Compiles but throws runtime exception

    • D.

      Compilation error

    Correct Answer
    D. Compilation error
    Explanation
    The code will result in a compilation error because the method "display()" is declared as private and can only be accessed within the same class. Since the method "show()" is a static method and is trying to call the private method "display()", it is not allowed and will result in a compilation error.

    Rate this question:

  • 3. 

    Consider the following code and choose the correct option: class A{      A(){         System.out.print("From A");     } } class B extends A{      B(int z){         z=2;     }     public static void main(String args[]){         new B(3);     } }

    • A.

      Compilation error

    • B.

      Comiples and prints From A

    • C.

      Compiles but throws runtime exception

    • D.

      Compiles and display 3

    Correct Answer
    B. Comiples and prints From A
    Explanation
    The code compiles and prints "From A" because the constructor of class A is called when an object of class B is created. The constructor of class B does not have any print statements, so only the print statement in the constructor of class A is executed.

    Rate this question:

  • 4. 

    Consider the following code and choose the correct option: class One{     int var1;     One (int x){         var1 = x;     } } class Derived extends One{     int var2;     Derived(){         super(1);     }     void display(){         System.out.println("var 1="+var1+", var 2="+var2);     } } class Main{     public static void main(String[] args){         Derived obj = new Derived();         obj.display();     } }

    • A.

      Compiles successfully but runtime error

    • B.

      Var 1=1 var 2=0

    • C.

      Compile error

    • D.

      Var 1=0 var 2=0

    Correct Answer
    B. Var 1=1 var 2=0
    Explanation
    The code compiles successfully because there are no syntax errors. However, at runtime, the program will encounter a NullPointerException when trying to access the variable var2 in the display() method. This is because var2 has not been initialized and has a default value of 0. Therefore, the output will be "var 1=1 var 2=0".

    Rate this question:

  • 5. 

    Consider the following code and choose the correct option: class Order{         Order(){             System.out.println("Cat");         }         public static void main(String... Args){             Order obj = new Order();             System.out.println("Ant");         }         static{             System.out.println("Dog");         }         {             System.out.println("Man");         }     }

    • A.

      Compile error

    • B.

      Man Dog Cat Ant

    • C.

      Dog Man Cat Ant

    • D.

      Cat Ant Dog Man

    Correct Answer
    C. Dog Man Cat Ant
    Explanation
    The correct answer is "Dog Man Cat Ant". This is because the static block is executed first, which prints "Dog". Then, the instance block is executed, which prints "Man". After that, the constructor is called, which prints "Cat". Finally, in the main method, "Ant" is printed.

    Rate this question:

  • 6. 

    Consider the following code and choose the correct option: public class MyAr {     public static void main(String argv[]) {         MyAr m = new MyAr();         m.amethod();     }     public void amethod() {         final int i1;         System.out.println(i1);     } }

    • A.

      Unresolved compilation problem: The local variable i1 may not have been initialized

    • B.

      Compilation and output of null

    • C.

      Compiled and output of 0

    • D.

      None of the given options

    Correct Answer
    A. Unresolved compilation problem: The local variable i1 may not have been initialized
    Explanation
    The code is attempting to print the value of the local variable "i1" without initializing it first. This results in a compilation error because local variables must be assigned a value before they can be used. Therefore, the correct answer is "Unresolved compilation problem: The local variable i1 may not have been initialized."

    Rate this question:

  • 7. 

    What would be the output? class MyClass1 {   private int area(int side)    {            return(side * side);     }     public static void main(String args[ ])     {            MyClass1 MC = new MyClass1( );            int area = MC.area(50);            System.out.println(area);     }  }

    • A.

      Compilation error

    • B.

      Runtime Exception

    • C.

      2500

    • D.

      50

    Correct Answer
    C. 2500
    Explanation
    The code defines a class MyClass1 with a private method area that takes an integer parameter side and returns the square of the side. In the main method, an object of MyClass1 is created and the area method is called with a parameter value of 50. The returned value is stored in a variable area and then printed. Since the area method correctly calculates the square of the side, the output will be 2500.

    Rate this question:

  • 8. 

    Consider the following code and choose the correct option: public class MyClass {  public static void main(String arguments[]) {   amethod(arguments);  }  public static void amethod(String[] arguments) {   System.out.println(arguments[0]);   System.out.println(arguments[1]);  } }

    • A.

      Prints Hi Hello

    • B.

      Compiler Error

    • C.

      Runs but no output

    • D.

      Runtime Error

    Correct Answer
    D. Runtime Error
    Explanation
    The given code is trying to access elements from the "arguments" array in the amethod() method. However, the code does not check if the array has at least two elements before accessing them. Therefore, if the "arguments" array does not have at least two elements, it will result in a runtime error.

    Rate this question:

  • 9. 

    Examine this code: String stringA = "Wild"; String stringB = " Irish"; String stringC = " Rose"; String result; Which of the following puts a reference to "Wild Irish Rose" in result?

    • A.

      Result = stringA.concat( stringB.concat( stringC ) );

    • B.

      Result.concat( stringA, stringB, stringC );

    • C.

      Result+stringA+stringB+stringC;

    • D.

      Result = concat(StringA).concat(StringB).concat(StringC)

    Correct Answer
    A. Result = stringA.concat( stringB.concat( stringC ) );
    Explanation
    The correct answer is `result = stringA.concat( stringB.concat( stringC ) );` because it concatenates `stringB` and `stringC` first using the `concat()` method, and then concatenates the result with `stringA` using the `concat()` method again. This creates the string "Wild Irish Rose" and assigns it to the `result` variable.

    Rate this question:

  • 10. 

    Carefully read the question and answer accordingly. A class can be declared as _______ if you do not want the class to be subclasses. Using the __________keyword we can abstract a class from its implementation

    • A.

      Protected, interface

    • B.

      Final, interface

    • C.

      Public, friend

    • D.

      Final, protected

    • E.

      Private, abstract

    Correct Answer
    B. Final, interface
    Explanation
    A class can be declared as "final" if you do not want the class to be subclasses. Using the "interface" keyword we can abstract a class from its implementation.

    Rate this question:

  • 11. 

    Consider the following code and choose the correct option: abstract class MineBase {     abstract void amethod();     static int i; } public class Mine extends MineBase {     public static void main(String argv[]){         int[] ar=new int[5];         for(i=0;i < ar.length;i++)             System.out.println(ar[i]);     } }

    • A.

      A Sequence of 5 zero's will be printed like 0 0 0 0 0

    • B.

      A Sequence of 5 one's will be printed like 1 1 1 1 1

    • C.

      IndexOutOfBoundes Error

    • D.

      Compilation Error occurs and to avoid them we need to declare Mine class as abstract

    Correct Answer
    D. Compilation Error occurs and to avoid them we need to declare Mine class as abstract
    Explanation
    The correct answer is that a compilation error occurs and to avoid it, the Mine class needs to be declared as abstract. This is because the Mine class is inheriting from the abstract class MineBase, which has an abstract method amethod(). In order for a class to inherit from an abstract class with abstract methods, the inheriting class must either provide an implementation for the abstract method or itself be declared as abstract. Since the Mine class does not provide an implementation for amethod(), it needs to be declared as abstract to avoid the compilation error.

    Rate this question:

  • 12. 

    What will be the result when you attempt to compile this program?  public class Rand{     public static void main(String argv[]){         int iRand;         iRand = Math.random();         System.out.println(iRand);             } }

    • A.

      Compile time error referring to a cast problem

    • B.

      A random number between 1 and 10

    • C.

      A random number between 0 and 1

    • D.

      A compile time error as random being an undefined method

    Correct Answer
    A. Compile time error referring to a cast problem
    Explanation
    The program will result in a compile time error referring to a cast problem. This is because the Math.random() method returns a double value between 0.0 and 1.0, and you are trying to assign it to an integer variable iRand without casting it. To fix this error, you should explicitly cast the double value to an integer using (int) before assigning it to iRand.

    Rate this question:

  • 13. 

    Which of the following declarations are correct? (Choose TWO)

    • A.

      Boolean b = TRUE;

    • B.

      Byte b = 256;

    • C.

      String s = “null”;

    • D.

      Int i = new Integer(“56”);

    Correct Answer(s)
    C. String s = “null”;
    D. Int i = new Integer(“56”);
    Explanation
    The correct answer is "String s = 'null';, int i = new Integer('56');". The first declaration is correct because it assigns the string value "null" to the variable s. The second declaration is correct because it creates a new Integer object with the value 56 and assigns it to the variable i. The other two declarations are incorrect. The first one should use lowercase 'true' instead of 'TRUE' to assign a boolean value, and the second one is incorrect because the byte data type can only hold values from -128 to 127, so assigning 256 is out of range.

    Rate this question:

  • 14. 

    Class A, B and C are in multilevel inheritance hierarchy respectively. In the main method of some other class if class C object is created, in what sequence the three constructors execute?

    • A.

      Constructor of A executes first, followed by the constructor of B and C

    • B.

      Constructor of C executes first followed by the constructor of A and B

    • C.

      Constructor of C executes first followed by the constructor of B and A

    • D.

      Constructor of A executes first followed by the constructor of C and B

    Correct Answer
    A. Constructor of A executes first, followed by the constructor of B and C
    Explanation
    In a multilevel inheritance hierarchy, the constructors of the classes are executed in a specific order. In this case, since class C is the most derived class, its constructor will execute first. After that, the constructor of class B, which is the immediate parent of class C, will execute. Finally, the constructor of class A, which is the parent of class B, will execute. Therefore, the correct sequence of constructor execution is: Constructor of C executes first, followed by the constructor of B and A.

    Rate this question:

  • 15. 

    Examine this code: class Base{     Base(){         int i = 100;         System.out.println(i);     } } public class Pri extends Base{     static int i = 200;     public static void main(String argv[]){         new Base();         System.out.println(i);     } }

    • A.

      100

    • B.

      100 200

    • C.

      200

    • D.

      Compile time error

    Correct Answer
    B. 100 200
    Explanation
    The code creates a class called Base with a constructor that initializes a local variable i with the value 100 and prints it. Then, a class called Pri extends Base and has a static variable i with the value 200. In the main method, a new instance of Base is created, which calls the constructor and prints 100. Then, the value of the static variable i is printed, which is 200. Therefore, the output will be "100 200".

    Rate this question:

  • 16. 

    Given, public class Q {     public static void main(String argv[]) {         int anar[] = new int[] { 1, 2, 3 };         System.out.println(anar[1]);     } }

    • A.

      Compiler Error: anar is referenced before it is initialized

    • B.

      2

    • C.

      1

    • D.

      Compiler Error: size of array must be defined

    Correct Answer
    B. 2
    Explanation
    The correct answer is 2 because the code initializes anar array with values {1, 2, 3} and then prints the value at index 1, which is 2.

    Rate this question:

  • 17. 

    Which statements, when inserted at (1), will not result in compile-time errors? [Choose Three] public class ThisUsage {     int planets;     static int suns;     public void gaze() {         int i;         // (1) INSERT STATEMENT HERE     } }

    • A.

      I = this.planets;

    • B.

      I = this.suns;

    • C.

      This = new ThisUsage();

    • D.

      This.i = 4;

    • E.

      This.suns = planets;

    Correct Answer(s)
    A. I = this.planets;
    B. I = this.suns;
    E. This.suns = planets;
    Explanation
    The statements "i = this.planets;" and "i = this.suns;" will not result in compile-time errors because "this" refers to the current instance of the class, and "planets" and "suns" are instance variables of the class. Therefore, it is valid to assign the values of these instance variables to the local variable "i".

    The statement "this.suns = planets;" will also not result in a compile-time error because "this.suns" refers to the static variable "suns" in the class, and "planets" is an instance variable. Assigning the value of the instance variable "planets" to the static variable "suns" is valid.

    The other statements "this = new ThisUsage();" and "this.i = 4;" will result in compile-time errors. Assigning a new instance of the class to "this" is not allowed, and there is no instance variable "i" in the class.

    Rate this question:

  • 18. 

    Given the following code what will be output?  public class Pass{     static int j=20;     public static void main(String argv[]){         int i=10;         Pass p = new Pass();         p.amethod(i);         System.out.println(i);         System.out.println(j);     }     public void amethod(int i) {         j+=(i+i);     } }

    • A.

      Error: amethod parameter does not match variable

    • B.

      10 and 40

    • C.

      10, and 20

    • D.

      20 and 40

    Correct Answer
    B. 10 and 40
    Explanation
    The code will output "10 and 40". In the main method, the variable i is initialized to 10. Then, a new instance of the Pass class is created and the amethod() method is called, passing in the value of i. In the amethod() method, the value of j is incremented by (i+i), which is 20. Then, in the main method, the value of i is printed, which is still 10. Finally, the value of j is printed, which is now 40.

    Rate this question:

  • 19. 

    Given, public class c123 {  private c123() {   System.out.println("Hellow");  }  public static void main(String args[]) {   c123 o1 = new c123();   c213 o2 = new c213();  } } class c213 {  private c213() {   System.out.println("Hello123");  } }

    • A.

      Hellow

    • B.

      It is not possible to declare a constructor as private

    • C.

      Compilation Error

    • D.

      Runs without any output

    Correct Answer
    C. Compilation Error
    Explanation
    The given code will result in a compilation error. This is because the constructor of the class c123 is declared as private, which means it cannot be accessed outside of the class. In the main method, when trying to create an object of c123 using the new keyword, it will throw a compilation error because the constructor is not accessible.

    Rate this question:

  • 20. 

    Here is the general syntax for method definition: accessModifier returnType methodName( parameterList ) {   Java statements   return returnValue; } What is true for the returnType and the returnValue?

    • A.

      The returnValue can be any type, but will be automatically converted to returnType when the method returns to the caller

    • B.

      If the returnType is void then the returnValue can be any type

    • C.

      The returnValue must be the same type as the returnType, or be of a type that can be converted to returnType without loss of information

    • D.

      The returnValue must be exactly the same type as the returnType.

    Correct Answer
    C. The returnValue must be the same type as the returnType, or be of a type that can be converted to returnType without loss of information
    Explanation
    The explanation for the given correct answer is that the returnType in a method definition specifies the type of value that the method will return. The returnValue, on the other hand, is the actual value that is returned by the method. According to the given answer, the returnValue must be the same type as the returnType or a type that can be converted to returnType without losing any information. This means that the method can return a value of the same type as returnType, or a value of a type that is compatible with returnType.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jul 03, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Aug 26, 2018
    Quiz Created by
    Marikannan
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.