Java Toughest Exam Quiz! Trivia

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 Raghu
R
Raghu
Community Contributor
Quizzes Created: 2 | Total Attempts: 1,028
| Attempts: 512 | Questions: 40
Please wait...
Question 1 / 40
0 %
0/100
Score 0/100
1. Runtime exception can be handled.

Explanation

Runtime exceptions can be handled in a program. Unlike checked exceptions, which are required to be declared or caught, runtime exceptions do not need to be explicitly handled. However, they can still be caught and handled using try-catch blocks. By handling runtime exceptions, the program can gracefully recover from the error and continue executing rather than abruptly terminating.

Submit
Please wait...
About This Quiz
Java Toughest Exam Quiz! Trivia - Quiz

Java Toughest Exam Quiz! Trivia challenges advanced Java programming skills, focusing on nested classes, method overriding, overloading, runtime exceptions, and numeric operations. Ideal for learners seeking to test their Java expertise in a rigorous format.

Tell us your name to personalize your report, certificate & get on the leaderboard!
2. Arrays can be used as variable arguments parameter type. 

Explanation

Arrays can be used as variable arguments parameter type. This means that a method can accept a variable number of arguments of the same type, and those arguments can be passed as an array. This is useful when the number of arguments is not known in advance, as it allows for flexibility in the number of arguments that can be passed to the method. By using an array as the variable arguments parameter type, the method can handle any number of arguments effectively.

Submit
3. Which of the following is an implementation of Map interface:

Explanation

Hashtable is an implementation of the Map interface because it stores key-value pairs and allows efficient retrieval of values based on their corresponding keys. It provides methods to add, remove, and retrieve elements using key operations. Hashtable also ensures that each key is unique and does not allow null keys or values. Therefore, Hashtable meets the requirements of the Map interface and can be used to store and manipulate data in a key-value format.

Submit
4. Constructors can be declared as private. 

Explanation

Constructors can be declared as private in order to restrict the creation of objects of a class to only within the class itself. This means that objects of the class cannot be created from outside the class, but can still be created within the class itself or by other methods or functions within the class. This can be useful in certain scenarios where control over object creation is necessary, such as implementing singleton patterns or factory methods.

Submit
5. Which of the following is true about packages?

Explanation

Packages in Java can contain both classes and interfaces. A package is a way to organize related classes and interfaces into a single unit. It provides a namespace for the classes and interfaces it contains, allowing them to be easily identified and accessed. By grouping classes and interfaces together in a package, it promotes code organization and modularity. This also allows for better code reuse and maintenance. Therefore, the statement "Packages can contain both Classes and Interfaces" is true.

Submit
6. Choose the correct option:

Explanation

Integer is a wrapper class because it is used to wrap the primitive data type int into an object. Wrapper classes in Java provide a way to use primitive data types as objects. Integer class provides various methods to perform operations on int values, such as converting int to String, parsing String to int, etc.

Submit
7. Consider the following code: Line no 1:class Outer{ Line no 2:public static class Inner{ Line no 3:} Line no 4:public static void display(){ } } Line no 5:public class Test Line no 6:{ Line no 7:public static void main(String args[]) Line no 8:{ Line no 9://Replace with code from the option below Line no 10:}} Which of the following option when replaced at line no 9,instantiates an instance of the nested class?

Explanation

The correct option at line no 9, "Outer.Inner o = new Outer.Inner();", instantiates an instance of the nested class. It creates an object of the inner class "Inner" using the syntax "Outer.Inner" and assigns it to the variable "o".

Submit
8. Which of the following option is not a valid wrapper type object creation?

Explanation

The given options are all examples of creating wrapper objects for primitive types. However, the statement "new Boolean("truth")" is not a valid way to create a Boolean wrapper object. The Boolean class in Java has two valid ways to create a Boolean object - either by passing a boolean value (true or false) to the constructor or by calling the valueOf() method. Therefore, the statement "new Boolean("truth")" is not a valid wrapper type object creation.

Submit
9. Which of the following statements is TRUE about StringBuffer class?

Explanation

The statement "StringBuffer is a mutable class" is true. A mutable class means that its contents can be modified after it is created. StringBuffer class in Java allows for the modification of its contents, such as appending or deleting characters, without creating a new object. This is in contrast to the String class, which is immutable and cannot be modified once created. StringBuffer is often used when there is a need to perform multiple modifications on a string, as it provides better performance and memory efficiency compared to creating multiple String objects.

Submit
10. Which is right?

Explanation

The correct answer is "Iterator i= HashMap.entrySet().Iterator();". This is because HashMap does not have a method called "Iterator()", so the first option is incorrect. The second option "Iterator i= HashMap.entrySet().Iterator();" is correct because it returns an iterator over the entries in the HashMap. The third option "Iterator i= HashMap.TreeSet().Iterator();" is incorrect because HashMap does not have a method called "TreeSet()".

Submit
11. Select the correct answer

Explanation

All the methods of an interface are by default abstract. This means that the methods declared in an interface do not have an implementation. They only provide a signature or a contract for the classes that implement the interface to follow. Any class that implements an interface must provide an implementation for all the methods declared in the interface. This allows for abstraction and polymorphism in object-oriented programming, as different classes can implement the same interface and provide their own specific implementation for the methods.

Submit
12. TreeSet uses interface to sort the data.

Explanation

The correct answer is Comparable. TreeSet is a class that implements the Set interface in Java. It uses the Comparable interface to sort the elements in a natural order. The Comparable interface provides a compareTo() method that allows objects to be compared and sorted based on their natural ordering. By implementing the Comparable interface, objects can define their own custom sorting logic. In the case of TreeSet, it uses the compareTo() method to determine the order of elements in the set.

Submit
13. The  following declaration(as a member variable) is legal. static final transient int maxElements =100;

Explanation

The given declaration is legal because it combines three modifiers: static, final, and transient. The static modifier means that the variable belongs to the class itself, rather than to any particular instance of the class. The final modifier means that the variable cannot be reassigned once it has been initialized. The transient modifier means that the variable will not be serialized when the object is serialized. Therefore, the declaration of "static final transient int maxElements = 100;" is legal.

Submit
14. Which of the following statements are true about finalize method?

Explanation

The finalize method in Java will always run before an object is garbage collected. This method is called by the garbage collector when it determines that there are no more references to the object. It allows the programmer to perform any necessary cleanup or deallocation of resources before the object is destroyed. However, it is important to note that the finalize method does not guarantee that the object will be garbage collected or that it will be collected immediately after the method is called.

Submit
15. Consider the following code: class Testone { public class Main { public static void main(String[] args) { int i=1; int n=++i%5; System.out.println("value of n is:" +n); n=i--%4; System.out.println("value of n is:" +n); n=i++%2; System.out.println("value of n is:" +n); } } Which of the following option gives the output for the above code?

Explanation

The code first increments the value of i by 1 and then calculates the remainder when i is divided by 5. Since i is 2 after the increment, the remainder is 2. Therefore, the first output is "value of n is: 2".

Next, the code decrements the value of i by 1 and calculates the remainder when i is divided by 4. Since i is 1 after the decrement, the remainder is 1. Therefore, the second output is "value of n is: 1".

Finally, the code increments the value of i by 1 and calculates the remainder when i is divided by 2. Since i is 2 after the increment, the remainder is 0. Therefore, the third output is "value of n is: 0".

Hence, the correct answer is 2,2,1.

Submit
16. Consider the following Statements: Statement A: Anonymous inner class can extend a class and implement an interface at the same time . Statement B: Anonymous class can have their own members. Which of the following option is true regarding the above statements?

Explanation

Both statements A and B are true. Statement A is true because an anonymous inner class can indeed extend a class and implement an interface simultaneously. This allows the anonymous inner class to inherit the properties and methods of the class it extends, while also implementing the methods of the interface it implements. Statement B is true because an anonymous class can have its own members, such as variables and methods, just like any other class.

Submit
17. Which of the following option gives the valid collection implementation class that implements the List interface and also provides the additional methods to get, add and remove elements from the head and tail of the list without specifying an index?

Explanation

LinkedList is the correct answer because it is a valid collection implementation class that implements the List interface. Additionally, LinkedList provides additional methods such as getFirst(), getLast(), addFirst(), addLast(), removeFirst(), and removeLast() which allow getting, adding, and removing elements from the head and tail of the list without specifying an index. ArrayList also implements the List interface, but it does not provide these additional methods. List and Collection are interfaces, not implementation classes.

Submit
18. Consider the following interface declaration: interface A{void main(String[] args);} interface B{public void main(String[] args);} interface C{public static void main(String[] args);}   interface D{protected void main(String[] args);} interface E{private void main(String[] args);} Which of the following option gives the valid interface declaration that will compile successfully?

Explanation

not-available-via-ai

Submit
19. Consider the following code: interface dumb {} interface Silent{} class Base implements dumb {} class Derived extends Base implements Silent {} public class Test { public static void main(String args[]) { Base[] base={new Base()};//Line no 1 Derived dev[]= {new Derived()};//Line no 2 Object obj =dev; //Line no 3 Base = obj;//Line no 4 } } At the time of compilation the above mentioned code generates some error. Which of the following option gives the line no where the error is generated?

Explanation

Line no 4 generates the error because we are trying to assign an Object type variable (obj) to a Base type variable, which is not allowed without casting. The obj variable is declared as Object type, so it can refer to any object. But when we try to assign it to a Base type variable, it requires an explicit type casting.

Submit
20. What is the output of the following code? 1 String str = "Welcome" 2 str.concat(" to Java!"); 3 System.out.println(str);

Explanation

The code declares a string variable "str" and assigns it the value "Welcome". The "concat" method is called on the string object "str" with the argument " to Java!". However, the "concat" method does not modify the original string, but instead returns a new string that is the concatenation of the original string and the argument. Since this returned string is not stored or printed, the output will still be the original value of "str", which is "Welcome".

Submit
21. What is the advantage of runtime polymorphism?

Explanation

Runtime polymorphism allows for code flexibility at runtime. This means that the behavior of an object can be determined at runtime based on its actual type, rather than its declared type. This allows for more dynamic and flexible code, as different objects can exhibit different behaviors even when accessed through a common interface or superclass. This flexibility enables easier modification and extension of code, as new behaviors can be added without modifying existing code. It also promotes code reuse, as different objects can share common behaviors through inheritance.

Submit
22. Consider the following code : class BigLength { public int getLength() {return 4;} } public class SmallLength extends BigLength { public long getLength() {return 5;} public static void main(String args[]) { BigLength s =new BigLength(); SmallLength sb=new SmallLength(); System.out.println(s.getLength()+","+sb.getLength() }; } } Which of the following option gives the valid output for the above code?

Explanation

The code will result in a compilation error because the method getLength() in the subclass SmallLength has a different return type (long) than the method getLength() in the superclass BigLength (int). In Java, method overriding requires the return type of the subclass method to be covariant with the return type of the superclass method. Since int and long are not covariant, the code will not compile.

Submit
23. Which statement is true for the class java.util.ArrayList?

Explanation

The statement that is true for the class java.util.ArrayList is that the elements in the collection are ordered. This means that the elements are stored in a specific sequence and can be accessed by their index position. The order in which elements are added to the ArrayList is maintained, allowing for easy retrieval and iteration over the elements in the collection.

Submit
24. HashMap is a Collection class. 

Explanation

The statement is true because HashMap is indeed a Collection class in Java. It is a part of the Java Collections Framework and implements the Map interface. HashMap stores key-value pairs and provides efficient retrieval and storage of elements based on the key. It allows null values and does not maintain the insertion order of elements. Overall, HashMap is a commonly used data structure for storing and accessing data in a key-value format.

Submit
25. Which of the following statement is false about for-each loop in Java?

Explanation

The statement "for-each loop can work only with generic collections" is false because the for-each loop can work with any type of array or collection, not just generic collections. It simplifies the process of iterating over elements in an array or collection by automatically handling the iteration and typecasting.

Submit
26. What will be the output of following code? Int a=2; Integer b=2; System.out.println(a==b);

Explanation

The code will output 1. This is because the "==" operator in Java compares the values of the variables. In this case, both "a" and "b" have the same value of 2, so the comparison evaluates to true and the code prints 1.

Submit
27. Which is true about an anonymous inner class?

Explanation

An anonymous inner class is a class that is defined and instantiated at the same time, without giving it a name. It can either extend exactly one class or implement exactly one interface. This allows for flexibility in creating a class that has specific behaviors or characteristics. The option that states "It can extend exactly one class or implement exactly one interface" accurately describes the capabilities of an anonymous inner class.

Submit
28. Which of the following are correct regarding method overriding?(Choose 2) 

Explanation

Method overriding is a feature in object-oriented programming where a subclass provides a specific implementation of a method that is already defined in its superclass. In order for method overriding to occur, both the methods must have the same name and the same signature, meaning they have the same return type and the same parameters. Additionally, the methods must be in different classes, with the subclass overriding the method from its superclass. Therefore, the correct answers are "Both the methods must not be in the same class" and "Same name same signature".

Submit
29. Which of the following correctly fits for the definition holding instances of other objects?

Explanation

The term "generic" correctly fits the definition of holding instances of other objects. In programming, a generic type allows for the creation of classes, methods, or interfaces that can work with different types of objects. It provides a way to create reusable code that can handle various data types without the need to specify them explicitly. This allows for greater flexibility and code reusability in situations where the specific type of the object may vary.

Submit
30. Consider the following statements: A. Every floating-point literal is implicitly a double, not a float. B. In the declaration byte b=120; int literal 120 is implicitly converted to byte. Which of the following option is valid regarding the above statements?

Explanation

Statement A is true because in Java, every floating-point literal is implicitly considered as a double by default. If we want to declare a float literal, we need to append an 'f' or 'F' at the end of the number.

Statement B is false because in the given declaration, the int literal 120 is not implicitly converted to byte. Instead, it is explicitly assigned to the byte variable 'b'. If the value exceeds the range of the byte data type, a compilation error will occur.

Submit
31. Consider the following code: public class ProblemsWorld { public static void main(String args[]) { try { xpect(); } catch(IOException e) { System.out.println("xpected caught"); } } public static void xpect() throws IOException { throw new FileNotFoundException(); } } Which of the following statement is true regarding the above code?

Explanation

The code compiles and runs successfully because the catch block catches the IOException thrown by the xpect() method. Since FileNotFoundException is a subclass of IOException, it is also caught by the catch block. Therefore, the code prints "xpected caught" without any errors.

Submit
32. Consider the following code: 1 public class FinallyCatch { 2 public static void main(String args[]) { 3 try { 4 throw new java.io.IOException(); 5 } 6 } 7 } Which of the following is true regarding the above code?

Explanation

The given code throws an IOException at line number 4. Since there is no catch block to handle this exception, the code demands a finally block at line number 5 to ensure that any necessary cleanup or resource releasing operations are performed before the program terminates.

Submit
33. Which of the following statement is true regarding method overloading?

Explanation

The statement "Methods can be overloaded across inherited classes" is true regarding method overloading. Method overloading allows multiple methods with the same name but different parameters to exist in the same class or in inherited classes. This means that when a class inherits from another class, it can have its own set of overloaded methods with the same name as the methods in the parent class. This allows for flexibility in the implementation of methods in inherited classes, as they can have different behaviors or handle different types of parameters while still sharing the same method name.

Submit
34. Consider the following code: Class MyError extends Error{ } Public class TestError { Public static void main(String args[]) { Try { Test(); } catch(Error ie) { System.out.println("Error caught"); } } Static void test() throws Error { Throw new MyError(); } } Which of the following option gives the output for the above code?

Explanation

The code will result in a compile time error because the catch block is trying to catch an object of type Error, which is not allowed. The catch block can only catch objects of types that are subclasses of Throwable, and Error is a direct subclass of Throwable. Therefore, the catch block cannot catch an Error object and a compile time error will occur.

Submit
35.  Consider the following code: 01 import java.util.Set; 02 import java.util.TreeSet; 03 04 class TestSet{ 05 public static void main(String[] args) { 06 Set set = new TreeSet(); 07 set.add("Green World"); 08 set.add(1); 09 set.add("Green Peace"); 10 System.out.println(set); 11 } 12 } Which of the following option gives the output for the above code?

Explanation

The code will throw a runtime exception because the elements in a TreeSet must be of the same type and implement the Comparable interface. In this case, the set contains both a string ("Green World") and an integer (1), which are not compatible types.

Submit
36. Which of the following returns a primitive data type?

Explanation

The methods Integer.intValue() and Integer.parseInt() both return primitive data types. Integer.intValue() returns the value of the Integer object as an int primitive type, while Integer.parseInt() returns the parsed integer value as an int primitive type.

Submit
37. Unboxing the Numeric Wrapper types to primitive types is done under operations. 

Explanation

Unboxing is the process of converting an object of a wrapper class to its corresponding primitive type. In this case, the question states that unboxing the numeric wrapper types to primitive types is done under certain operations. The operations mentioned are ++ (increment), - (subtraction), and == (equality). These operations require the conversion of the wrapper types to their primitive counterparts in order to perform the desired operation.

Submit
38. Which of the following statements are true regarding try-catch-finally?

Explanation

A catch block can have another try block nested inside: This statement is true. In Java, a catch block can contain another try block to handle nested exceptions within the catch block.

An exception which is not handled by a catch block will be handled by subsequent catch blocks: This statement is true. If an exception is not caught and handled by a catch block, it will be passed to the next catch block in the sequence until it is either caught or reaches the end of the try-catch-finally structure.

Finally block cannot have a try block with multiple catch blocks: This statement is false. The finally block can indeed have a try block with multiple catch blocks. This allows for handling different types of exceptions within the same finally block.

An exception which is not handled by a catch block can be handled by writing another try-catch block inside finally block: This statement is false. Exceptions should be handled within the catch block, not within the finally block. Placing a try-catch block inside the finally block is not a recommended practice and can lead to unexpected behavior.

Submit
39. Which of the following options are the methods NOT available in StringBuffer class? 

Explanation

The methods append(short s) and append(byte b) are not available in the StringBuffer class. The StringBuffer class provides various append() methods for appending different types of data such as int, boolean, and long, but it does not have specific methods for appending short or byte data types.

Submit
40. Which of the following are true regarding RuntimeException? 

Explanation

Any class that derives the RuntimeException will always be an unchecked exception because RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method's throws clause or caught using a try-catch block. Therefore, RuntimeException does not require a throws declaration.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 21, 2023 +

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

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jan 03, 2017
    Quiz Created by
    Raghu
Cancel
  • All
    All (40)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Runtime exception can be handled.
Arrays can be used as variable arguments parameter type. 
Which of the following is an implementation of Map interface:
Constructors can be declared as private. 
Which of the following is true about packages?
Choose the correct option:
Consider the following code: ...
Which of the following option is not a valid wrapper type object...
Which of the following statements is TRUE about StringBuffer class?
Which is right?
Select the correct answer
TreeSet uses interface to sort the data.
The  following declaration(as a member variable) is legal....
Which of the following statements are true about finalize method?
Consider the following code:...
Consider the following Statements:...
Which of the following option gives the valid collection...
Consider the following interface declaration:...
Consider the following code:...
What is the output of the following code?...
What is the advantage of runtime polymorphism?
Consider the following code :...
Which statement is true for the class java.util.ArrayList?
HashMap is a Collection class. 
Which of the following statement is false about for-each loop in Java?
What will be the output of following code? ...
Which is true about an anonymous inner class?
Which of the following are correct regarding method overriding?(Choose...
Which of the following correctly fits for the definition holding...
Consider the following statements:...
Consider the following code:...
Consider the following code:...
Which of the following statement is true regarding method overloading?
Consider the following code:...
 Consider the following code:...
Which of the following returns a primitive data type?
Unboxing the Numeric Wrapper types to primitive types is done under...
Which of the following statements are true regarding...
Which of the following options are the methods NOT available in...
Which of the following are true regarding RuntimeException? 
Alert!

Advertisement