Java Trivia Test 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 323868
3
323868
Community Contributor
Quizzes Created: 1 | Total Attempts: 3,722
Questions: 38 | Attempts: 3,722

SettingsSettingsSettings
Java Trivia Test Quiz - Quiz


Java is one of the most popular computer programming languages. If you've studied about it, then try this Java trivia test quiz and see if you can pass this test or not. This might also give you the chance to revise your concepts. And remember that in order to pass this test, you need to score at least more than 70% marks. Do you think you can do it? Give it a shot and let's see how many correct answers you can give. Good luck!


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 answer is "Outer.Inner o = new Outer.Inner();". This statement creates an instance of the nested class Inner by using the syntax "Outer.Inner". The "new" keyword is used to create a new instance of the class, and the outer class "Outer" is specified before the nested class "Inner" to indicate the nesting relationship.

    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 (the one in the superclass and the one in the subclass) must have the same name and the same signature. However, it is important to note that the methods must not be in the same class. This is because method overriding is a way to provide a different implementation for a method in a subclass, and if both methods were in the same class, it would not be considered overriding but rather overloading.

    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
    Methods can be overloaded across inherited classes means that a subclass can have its own version of an overloaded method that is already present in its superclass. This allows the subclass to provide a different implementation of the method while still maintaining the same name and parameter types. This feature enables polymorphism and allows for more flexibility and customization in the behavior of inherited classes.

    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 must be declared or caught, runtime exceptions do not require explicit handling. However, it is considered good practice to handle runtime exceptions to prevent the program from crashing or exhibiting unexpected behavior. This can be done by using try-catch blocks to catch and handle the specific runtime exceptions that may occur during the execution of the program.

    Rate this question:

  • 5. 

    Unboxing the Numeric Wrapper types to primitive types is done under operations (choose 3) 

    • 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, unboxing the numeric wrapper types to primitive types can be done using the increment operator (++), the subtraction operator (-), and the equality operator (==). The increment operator is used to increase the value by 1, the subtraction operator is used to perform arithmetic subtraction, and the equality operator is used to compare two values for equality.

    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 option "new Boolean("truth");" is not a valid wrapper type object creation because the constructor for the Boolean class only accepts the values "true" or "false" as arguments. Using any other value, such as "truth", will result in a compilation error.

    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 compile without any errors. The output of the code will be 1. This is because the "==" operator is used to compare the values of the variables. In this case, the value of the variable "a" is 2 and the value of the variable "b" is also 2. Since both variables have the same value, the expression "a==b" will evaluate to true, which will be printed as 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
    Generic correctly fits the definition of holding instances of other objects. In programming, generic refers to a type that can be used with different data types. It allows for the creation of classes, methods, or interfaces that can work with different types of objects, providing flexibility and reusability. With generics, we can create a single piece of code that can handle multiple types of objects, making it a suitable choice for holding instances of other objects.

    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 instance of the Error class, which is not allowed. The Error class is a subclass of the Throwable class, and it is not intended to be caught by user code. Therefore, the code will not compile.

    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 it is attempting to add different types of objects (a String and an Integer) to the Set, which is not allowed. The TreeSet class in Java requires all elements to be of the same type, so trying to add different types will result in a ClassCastException.

    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
    An interface is a collection of abstract methods that define a contract for classes to implement. By default, all the methods declared in an interface are abstract, meaning they do not have a body or implementation. This is because interfaces are meant to provide a blueprint for classes to follow, rather than providing specific functionality. Therefore, the statement "All the methods of an interface are by default abstract" is correct.

    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 can iterate over arrays, lists, sets, and other types of collections.

    Rate this question:

  • 13. 

    HashMap is a Collection class. State TRUE or FALSE.

    • 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, which is a sub-interface of the Collection interface. HashMap is used to store key-value pairs and provides efficient retrieval and storage of elements based on their keys.

    Rate this question:

  • 14. 

    Which of the following are true regarding RuntimeException? (Choose 2)

    • 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 is a subclass of Exception and all subclasses of RuntimeException are unchecked exceptions. RuntimeException does not require a throws declaration because it is an unchecked exception and does not need to be declared in the method signature or handled explicitly.

    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
    Explanation
    The correct answer is interface A,B because both interface A and interface B have a valid method declaration that will compile successfully. Interface A has a method main with the default access modifier, and interface B has a method main with the public access modifier.

    Rate this question:

  • 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 because a mutable class can be modified after its creation. StringBuffer allows for the modification of its content, 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 changed once created.

    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 is called by the garbage collector before an object is garbage collected. It provides an opportunity for the programmer to perform any necessary cleanup or resource deallocation operations before the object is destroyed. However, it is important to note that the finalize method does not directly free memory allocated to an object. The garbage collector is responsible for reclaiming the memory occupied by unreachable objects.

    Rate this question:

  • 18. 

    Which of the following statements are true regarding try-catch-finally? (Choose 2)

    • 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. It is possible to have a try block nested inside a catch block. This allows for additional error handling 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 handled by a catch block, it will be passed on to the next catch block in the sequence. This allows for multiple catch blocks to handle different types of exceptions.

    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 together. By placing them in the same package, they can be easily accessed and used within the package without the need for import statements. This allows for better organization and encapsulation of code. 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 are true. An anonymous inner class can extend a class and implement an interface simultaneously. This allows for combining the functionalities of both the extended class and implemented interface in the anonymous inner class. Additionally, anonymous classes can have their own members, including fields, methods, and constructors. This allows for customization and specific behavior within the anonymous 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. It also provides additional methods such as getFirst(), getLast(), addFirst(), addLast(), removeFirst(), and removeLast() which allow elements to be accessed and modified from both the head and tail of the list without specifying an index.

    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 correct answer is "Demands a finally block at line number 5". This is because the code in the try block throws an IOException, which is a checked exception. According to Java's exception handling rules, when a checked exception is thrown, it must either be caught using a catch block or declared to be thrown by the method using the throws keyword. In this case, there is no catch block to handle the IOException, so the code demands a finally block at line number 5 to handle any necessary cleanup operations before the exception is propagated further.

    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 `SmallLength` class has a different return type (`long`) compared to its superclass (`int`). In Java, when overriding a method, the return type must be the same or a subtype of the return type in the superclass. Since `long` is not a subtype of `int`, the code will not compile.

    Rate this question:

  • 24. 

    Which of the following options are the methods NOT available in StringBuffer class? (Choose 2)

    • 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 methods to append various data types 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: 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 will compile and run successfully because the main method catches the IOException that is thrown by the xpect() method. When the xpect() method is called, it throws a FileNotFoundException, which is a subclass of IOException. Since the catch block in the main method catches IOException, it will also catch the FileNotFoundException. Therefore, the code will print "xpected caught" and continue to run without any errors.

    Rate this question:

  • 26. 

    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 an error because we are trying to assign an object of type Object to a variable of type Base, which is not allowed. The variable "obj" is declared as an Object, and it cannot be directly assigned to a variable of type Base without explicit casting.

    Rate this question:

  • 27. 

    Arrays can be used as variable arguments parameter type. State True or False. 

    • 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, and one of the arguments can be an array. The array can then be accessed and manipulated within the method. This allows for flexibility in the number of arguments passed to the method, as well as the ability to work with arrays of different sizes. Therefore, the statement "Arrays can be used as variable arguments parameter type" is true.

    Rate this question:

  • 28. 

    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 correct answer is that the elements in the collection are ordered. This means that the elements in an ArrayList are stored in a specific order and can be accessed by their index. Unlike some other collection classes, the order of elements in an ArrayList is maintained and will not change unless explicitly modified by the programmer.

    Rate this question:

  • 29. 

    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 output of the code will be "Welcome". This is because the concat() method in line 2 does not modify the original string "str", but instead returns a new string that is the concatenation of "str" and " to Java!". However, this new string is not assigned to any variable or printed, so the original string "str" remains unchanged.

    Rate this question:

  • 30. 

    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 the keywords static, final, and transient with the data type int and the variable name maxElements. The keyword static means that the variable is associated with the class itself rather than with any specific instance of the class. The keyword final means that the variable's value cannot be changed once it is assigned. The keyword transient means that the variable is not serialized when the object is serialized. Since all these keywords are valid for member variables, the given declaration is legal.

    Rate this question:

  • 31. 

    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
    The given statement is correct. Integer is indeed a wrapper class in Java. Wrapper classes are used to convert primitive data types into objects. Integer is the wrapper class for the int data type. It provides methods and functionality to manipulate and perform operations on int values.

    Rate this question:

  • 32. 

    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 allows key-value pairs to be stored and accessed. It provides methods to add, remove, and retrieve elements based on their keys. Hashtable also ensures that keys are unique and does not allow null keys or values. It is a synchronized collection, making it thread-safe for use in multi-threaded environments.

    Rate this question:

  • 33. 

    Which of the following returns a primitive data type?(choose 2)

    • 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 of a string as an int primitive type.

    Rate this question:

  • 34. 

    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 changed dynamically during program execution. This flexibility is achieved through method overriding, where a subclass can provide its own implementation of a method defined in its superclass. This allows for different objects to be treated interchangeably, enhancing code flexibility and making it easier to modify and extend the functionality of a program without impacting existing code.

    Rate this question:

  • 35. 

    Constructors can be declared as private. State true or false.

    • 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 from outside the class itself. This can be useful in certain scenarios where the class wants to control the creation of its own objects and prevent direct instantiation by other classes or external code. By declaring the constructor as private, only methods within the class can create objects of that class, ensuring that the class has full control over the object creation process.

    Rate this question:

  • 36. 

    TreeSet uses interface to sort the data

    • A.

      Serializable

    • B.

      SortTable

    • C.

      Comparable

    Correct Answer
    C. Comparable
    Explanation
    The correct answer is Comparable. TreeSet uses the Comparable interface to sort the data. This interface allows objects to define their natural ordering by implementing the compareTo() method. By implementing Comparable, objects can be compared and sorted based on their own defined criteria. TreeSet uses this compareTo() method to arrange the elements in ascending order according to the natural ordering defined by the objects.

    Rate this question:

  • 37. 

    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 the entrySet() method returns a Set view of the mappings contained in the HashMap, and calling the iterator() method on this Set returns an iterator over the entries. Therefore, this line of code correctly initializes the iterator to iterate over the entries of the HashMap. The other options are incorrect because they either do not exist (HashMap.Iterator()) or do not return an iterator over the entries of the HashMap (HashMap.TreeSet().Iterator()).

    Rate this question:

  • 38. 

    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
    The given answer states that only statement A is true. This means that every floating-point literal is implicitly a double, not a float. This is true because in Java, by default, floating-point literals are treated as double unless specified otherwise. Statement B, on the other hand, is false. In the given declaration byte b=120;, the int literal 120 is not implicitly converted to byte. Instead, a type casting is required to explicitly convert the int literal to byte.

    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
  • May 29, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 20, 2012
    Quiz Created by
    323868
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.