Hardest Java Exam Trivia Quiz!

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Marikannan
M
Marikannan
Community Contributor
Quizzes Created: 2 | Total Attempts: 5,976
| Attempts: 5,821 | Questions: 20
Please wait...
Question 1 / 20
0 %
0/100
Score 0/100
1. 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);     }  }

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.

Submit
Please wait...
About This Quiz
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... see moretest with a good score? Do you have the confidence? Let's see how well you perform in this amazing quiz.
see less

Personalize your quiz and earn a certificate with your name on it!
2. Given, public class Q {     public static void main(String argv[]) {         int anar[] = new int[] { 1, 2, 3 };         System.out.println(anar[1]);     } }

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.

Submit
3. 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);     } }

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".

Submit
4. 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?

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.

Submit
5. 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);     } }

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."

Submit
6. 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);     } }

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.

Submit
7. 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]);     } }

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.

Submit
8. 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?

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.

Submit
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?

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.

Submit
10. 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");  } }

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.

Submit
11. 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();     } }

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.

Submit
12. 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");         }     }

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.

Submit
13. 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();     } }

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".

Submit
14. 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

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.

Submit
15. 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);     } }

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.

Submit
16. 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);             } }

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.

Submit
17. 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();     } }

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.

Submit
18. 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]);  } }

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.

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

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.

Submit
20. 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     } }

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.

Submit
View My Results

Quiz Review Timeline (Updated): Jul 3, 2023 +

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
Cancel
  • All
    All (20)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What would be the output? ...
Given, ...
Examine this code: ...
Class A, B and C are in multilevel inheritance hierarchy respectively....
Consider the following code and choose the correct option: ...
Given the following code what will be output?  ...
Consider the following code and choose the correct option: ...
Here is the general syntax for method definition: ...
Examine this code: ...
Given, ...
Consider the following code and choose the correct option: ...
Consider the following code and choose the correct option: ...
Consider the following code and choose the correct option: ...
Carefully read the question and answer accordingly. ...
Consider the following code and choose the correct option: ...
What will be the result when you attempt to compile this...
Consider the following code and choose the correct option: ...
Consider the following code and choose the correct option: ...
Which of the following declarations are correct? (Choose TWO)
Which statements, when inserted at (1), will not result in...
Alert!

Advertisement