Java Toughest Exam Quiz! Trivia

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 Raghu
R
Raghu
Community Contributor
Quizzes Created: 2 | Total Attempts: 894
Questions: 40 | Attempts: 509

SettingsSettingsSettings
Java Toughest Exam Quiz! Trivia - Quiz

.


Questions and Answers
  • 1. 

    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?

    • A.

      Outer o = new Outer(); Outer.Inner oi = o.new Outer.Inner();

    • B.

      Outer.Inner o = new Outer.Inner();

    • C.

      Outer.Inner oi = new Inner();

    • D.

      Inner oi = new Outer.Inner();

    Correct Answer
    B. Outer.Inner o = new Outer.Inner();
    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".

    Rate this question:

  • 2. 

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

    • A.

      Both the methods must not be in the same class

    • B.

      Same name same signature

    • C.

      Same name different signature

    • D.

      Both the methods must be in the same class

    Correct Answer(s)
    A. Both the methods must not be in the same class
    B. Same name same signature
    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".

    Rate this question:

  • 3. 

    Which of the following statement is true regarding method overloading?

    • A.

      Static methods cannot be overloaded

    • B.

      Overloaded methods should always return same type of value

    • C.

      Overloaded methods cannot be declared as abstract

    • D.

      Methods can be overloaded across inherited classes

    Correct Answer
    D. Methods can be overloaded across inherited classes
    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.

    Rate this question:

  • 4. 

    Runtime exception can be handled.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 5. 

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

    • A.

      ++

    • B.

      +

    • C.

      -

    • D.

      =

    • E.

      ==

    Correct Answer(s)
    A. ++
    C. -
    E. ==
    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.

    Rate this question:

  • 6. 

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

    • A.

      New Boolean(“truth”);

    • B.

      New Long(“3465”);

    • C.

      New Integer(“637”);

    • D.

      New Character(“B”);

    • E.

      New Byte(“10”);

    Correct Answer
    A. New Boolean(“truth”);
    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.

    Rate this question:

  • 7. 

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

    • A.

      0

    • B.

      Compile time error

    • C.

      1

    • D.

      Run time error

    Correct Answer
    C. 1
    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.

    Rate this question:

  • 8. 

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

    • A.

      Polymorphic

    • B.

      Generic

    • C.

      Composition

    • D.

      Aggregation

    Correct Answer
    B. Generic
    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.

    Rate this question:

  • 9. 

    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?

    • A.

      Run time error test() method does not throw an error type instance

    • B.

      Compile time error Cannot catch Error type objects

    • C.

      Compile time error Error class cannot be extended

    • D.

      Prints Error caught

    Correct Answer
    B. Compile time error Cannot catch Error type objects
    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.

    Rate this question:

  • 10. 

     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?

    • A.

      Compilation error at line no 8

    • B.

      Throws Runtime Exception

    • C.

      Prints the output [Green World, 1, Green Peace] at line no 9

    • D.

      Prints the output [Green World, Green Peace] at line no 9

    Correct Answer
    B. Throws Runtime Exception
    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.

    Rate this question:

  • 11. 

    Select the correct answer

    • A.

      An interface can implement another interface

    • B.

      An interface can be instantiated

    • C.

      All the methods of an interface are by default abstract

    • D.

      An interface can contain concrete methods

    Correct Answer
    C. All the methods of an interface are by default abstract
    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.

    Rate this question:

  • 12. 

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

    • A.

      For-each loop does the automatic typecasting

    • B.

      For-each loop is an alternative to Enumeration

    • C.

      For-each loop is an alternative to Iterator

    • D.

      For-each loop can work only with generic collections

    Correct Answer
    D. For-each loop can work only with generic collections
    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.

    Rate this question:

  • 13. 

    HashMap is a Collection class. 

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 14. 

    Which of the following are true regarding RuntimeException? 

    • A.

      RuntimeException can be handled using a catch that handles Error

    • B.

      Any class that derives the RuntimeException will always be an unchecked exception

    • C.

      RuntimeException does not require a throws declaration

    • D.

      If RuntimeException is declared using throws clause, then the calling methods should handle it using try –catch block

    Correct Answer(s)
    B. Any class that derives the RuntimeException will always be an unchecked exception
    C. RuntimeException does not require a throws declaration
    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.

    Rate this question:

  • 15. 

    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?

    • A.

      Interface A,B,C

    • B.

      Interface B,C,D

    • C.

      Interface B,C

    • D.

      Interface A,B

    Correct Answer
    D. Interface A,B
  • 16. 

    Which of the following statements is TRUE about StringBuffer class?

    • A.

      StringBuffer can be extended, since it is mutable

    • B.

      StringBuffer is a mutable class

    • C.

      StringBuffer is a sub class of String class

    • D.

      StringBuffer is a Wrapper to the existing String class

    Correct Answer
    B. StringBuffer is a mutable 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.

    Rate this question:

  • 17. 

    Which of the following statements are true about finalize method?

    • A.

      Finalize will run when an object becomes unreachable

    • B.

      Finalize allows a programmer to free memory allocated to an object

    • C.

      Finalize may run before or after an object is garbage collected

    • D.

      Finalize will always run before an object is garbage collected

    Correct Answer
    D. Finalize will always run before an object is garbage collected
    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.

    Rate this question:

  • 18. 

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

    • A.

      A catch block can have another try block nested inside

    • B.

      An exception which is not handled by a catch block will be handled by subsequent catch blocks

    • C.

      Finally block cannot have a try block with multiple catch blocks

    • D.

      An exception which is not handled by a catch block can be handled by writing another try catch block inside finally block

    Correct Answer(s)
    A. A catch block can have another try block nested inside
    B. An exception which is not handled by a catch block will be handled by subsequent catch blocks
    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.

    Rate this question:

  • 19. 

    Which of the following is true about packages?

    • A.

      Class and Interfaces in the sub packages will be automatically available to the outer packages without using import statement.

    • B.

      Packages can contain both Classes and Interfaces

    • C.

      Packages can contain only Java Source files

    • D.

      Sub packages should be declared as private in order to deny importing them

    Correct Answer
    B. Packages can contain both Classes and Interfaces
    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.

    Rate this question:

  • 20. 

    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?

    • A.

      Both the statements are true

    • B.

      Statement B is true and A is false

    • C.

      Both the statement are false

    • D.

      Statement A is true and B is false

    Correct Answer
    A. Both the statements are true
    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.

    Rate this question:

  • 21. 

    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?

    • A.

      LinkedList

    • B.

      ArrayList

    • C.

      List

    • D.

      Collection

    Correct Answer
    A. LinkedList
    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.

    Rate this question:

  • 22. 

    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?

    • A.

      Demands a finally block at line number 4

    • B.

      Shows unhandled exception type IOException at line number 5

    • C.

      Demands a finally block at line number 5

    Correct Answer
    C. Demands a finally block at line number 5
    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.

    Rate this question:

  • 23. 

    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?

    • A.

      4,5

    • B.

      Compilation error

    • C.

      4,4

    • D.

      5,5

    • E.

      5,4

    Correct Answer
    B. Compilation error
    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.

    Rate this question:

  • 24. 

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

    • A.

      Append(short s)

    • B.

      Append(byte b)

    • C.

      Append(int i)

    • D.

      Append(boolean b)

    • E.

      Append(long l)

    Correct Answer(s)
    A. Append(short s)
    B. Append(byte b)
    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.

    Rate this question:

  • 25. 

    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?

    • A.

      1,2,2

    • B.

      2,2,2

    • C.

      0.2,0.5,0.5

    • D.

      2,2,1

    Correct Answer
    D. 2,2,1
    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.

    Rate this question:

  • 26. 

    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?

    • A.

      Compile time error; built-in-exceptions like FileNotFoundException cannot be instantiated programmatically

    • B.

      Compiles successfully.But throws runtime error while executing

    • C.

      Compile time error; the declaration does not match the throw statement

    • D.

      Compiles and Runs successfully and prints “xpected caught”

    Correct Answer
    D. Compiles and Runs successfully and prints “xpected caught”
    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.

    Rate this question:

  • 27. 

    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?

    • A.

      Line no 1

    • B.

      Line no 4

    • C.

      Line no 3

    • D.

      Line no 2

    Correct Answer
    B. Line no 4
    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.

    Rate this question:

  • 28. 

    Arrays can be used as variable arguments parameter type. 

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 29. 

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

    • A.

      The elements in the collection are ordered.

    • B.

      The collection is guaranteed to be immutable.

    • C.

      The elements in the collection are guaranteed to be unique.

    • D.

      The elements in the collection are accessed using a unique key.

    Correct Answer
    A. The elements in the collection are ordered.
    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.

    Rate this question:

  • 30. 

    Which is true about an anonymous inner class?

    • A.

      It can extend exactly one class and implement exactly one interface.

    • B.

      It can extend exactly one class and can implement multiple interfaces.

    • C.

      It can extend exactly one class or implement exactly one interface.

    • D.

      It can implement multiple interfaces regardless of whether it also extends a class.

    Correct Answer
    C. It can extend exactly one class or implement exactly one interface.
    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.

    Rate this question:

  • 31. 

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

    • A.

      String are immutable, compilation error at line 2.

    • B.

      String are immutable, runtime exception at line 2.

    • C.

      Prints “Welcome”.

    • D.

      Prints “Welcome to Java!”

    Correct Answer
    C. Prints “Welcome”.
    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".

    Rate this question:

  • 32. 

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

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 33. 

    Choose the correct option:

    • A.

      StringBuffer is a wrapper class

    • B.

      Integer is a wrapper class

    • C.

      Wrapper class contains no methods

    • D.

      String is a wrapper class

    Correct Answer
    B. Integer is a wrapper class
    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.

    Rate this question:

  • 34. 

    Which of the following is an implementation of Map interface:

    • A.

      TreeSet

    • B.

      ArrayList

    • C.

      Hashtable

    • D.

      Vector

    Correct Answer
    C. Hashtable
    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.

    Rate this question:

  • 35. 

    Which of the following returns a primitive data type?

    • A.

      Integer.decode()

    • B.

      Integer.intValue()

    • C.

      Integer.getInteger()

    • D.

      Integer.valueOf()

    • E.

      Integer.parseInt()

    Correct Answer(s)
    B. Integer.intValue()
    E. Integer.parseInt()
    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.

    Rate this question:

  • 36. 

    What is the advantage of runtime polymorphism?

    • A.

      Efficient utilization of memory at runtime

    • B.

      Code flexibility at runtime

    • C.

      Avoiding method name confusion at runtime

    • D.

      Code reuse

    Correct Answer
    B. Code flexibility at runtime
    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.

    Rate this question:

  • 37. 

    Constructors can be declared as private. 

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 38. 

    TreeSet uses interface to sort the data.

    • A.

      Serializable

    • B.

      SortTable

    • C.

      Comparable

    Correct Answer
    C. Comparable
    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.

    Rate this question:

  • 39. 

    Which is right?

    • A.

      Iterator i= HashMap.Iterator();

    • B.

      Iterator i= HashMap.entrySet().Iterator();

    • C.

      Iterator i= HashMap.TreeSet().Iterator();

    Correct Answer
    B. Iterator i= HashMap.entrySet().Iterator();
    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()".

    Rate this question:

  • 40. 

    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?

    • A.

      Both A and B are true

    • B.

      Both A and B are false

    • C.

      Only A is true

    • D.

      Only B is true

    Correct Answer
    C. Only A is true
    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.

    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
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Jan 03, 2017
    Quiz Created by
    Raghu
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.