Choose Correct Answer Core Java

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 Suhaana
S
Suhaana
Community Contributor
Quizzes Created: 1 | Total Attempts: 701
Questions: 25 | Attempts: 701

SettingsSettingsSettings
Core Java Quizzes & Trivia

Pls do the correction if the answer is wrong in core java.


Questions and Answers
  • 1. 

    Which of the following may override a method whose signature is void xyz (float f)?1.void xyz(float f)2.public void xyz(float f3,private void xyz(float f)

    • A.

      1, 2

    • B.

      2, 3

    • C.

      3

    • D.

      1,3

    Correct Answer
    C. 3
    Explanation
    The correct answer is 3 because a method with a private access modifier can only be accessed within the same class and cannot be overridden in a subclass. Therefore, option 3, which has a private access modifier, can override the method void xyz(float f).

    Rate this question:

  • 2. 

    2, 31 An anonymous inner class may implement at most one interface.2 An anonymous inner class may implement arbitrarily many interfaces.3 An anonymous inner class may extend a parent class other than Object.

    • A.

      2, 3

    • B.

      1,3

    • C.

      3

    • D.

      1,2

    Correct Answer
    B. 1,3
    Explanation
    An anonymous inner class in Java can implement at most one interface, as stated in option 1. This means that it can only provide the implementation for the methods defined in a single interface. However, an anonymous inner class can also extend a parent class other than Object, as mentioned in option 3. This allows the anonymous inner class to inherit the properties and methods of the parent class. Therefore, the correct answer is 1,3.

    Rate this question:

  • 3. 

    Which of the following are valid declarations? 1 int $x; 2 int 123; 3 int _123;

    • A.

      1,2

    • B.

      2,3

    • C.

      1,3

    • D.

      None of the above

    Correct Answer
    C. 1,3
    Explanation
    The correct answer is 1,3. In option 1, int $x; is a valid declaration because it starts with a letter or an underscore. In option 2, int 123; is not a valid declaration because it starts with a number. In option 3, int _123; is a valid declaration because it starts with an underscore. Therefore, options 1 and 3 are the valid declarations.

    Rate this question:

  • 4. 

    Which line if inserted independently at line 6, will compile in the code given below? 1. class Voop {2. public static void main(String[] args) {3. doStuff(1);4. doStuff(1,2);5. }6. // insert code here7. } 1. static void doStuff(int... doArgs) { } 2. static void doStuff(int[] doArgs) { }3. static void doStuff(int x, int... doArgs) { }

    • A.

      1,3

    • B.

      2,3

    • C.

      1,2,3

    • D.

      None of the above

    Correct Answer
    C. 1,2,3
    Explanation
    If we insert the code from option 1 at line 6, it will compile because it is a valid method declaration that accepts a variable number of arguments of type int.
    If we insert the code from option 2 at line 6, it will also compile because it is a valid method declaration that accepts an array of type int.
    If we insert the code from option 3 at line 6, it will compile because it is a valid method declaration that accepts an int followed by a variable number of arguments of type int. Therefore, options 1, 2, and 3 can all be inserted independently at line 6 and will compile.

    Rate this question:

  • 5. 

    What value does readLine() return when it has reached the end of the file?

    • A.

      1

    • B.

      -1

    • C.

      Null

    • D.

      0

    Correct Answer
    C. Null
    Explanation
    The correct answer is "Null". When the readLine() function has reached the end of the file, it returns a null value. This indicates that there are no more lines to be read from the file.

    Rate this question:

  • 6. 

    Which of the following statements are true? 1 The invocation of an object’s finalize () method is always the last thing that happens before an object is garbage collected (GCed). 2 When a stack variable goes out of scope, it is eligible for GC. 3 Some reference variables live on the stack, and some live on the heap.

    • A.

      1

    • B.

      2

    • C.

      None of above

    • D.

      3

    Correct Answer
    B. 2
    Explanation
    When a stack variable goes out of scope, it means that it is no longer accessible or in use by the program. At this point, the variable becomes eligible for garbage collection because it is no longer needed. This is true because stack variables are automatically deallocated when they go out of scope, while heap variables require manual deallocation. Therefore, statement 2 is true.

    Rate this question:

  • 7. 

    Which of the following statements are true about reference variables? 1 A reference variable type can be changed. 2 A reference is a variable, so it can be reassigned to other objects (unless the reference is declared final). 3 A reference variable's type determines the methods that can be invoked on the object the variable is referencing.

    • A.

      1,2

    • B.

      3

    • C.

      2,3

    • D.

      1

    Correct Answer
    C. 2,3
    Explanation
    A reference variable's type determines the methods that can be invoked on the object the variable is referencing. This is because the type of the reference variable determines the available methods and properties of the object it refers to.

    A reference is a variable, so it can be reassigned to other objects (unless the reference is declared final). This means that the reference variable can be pointed to a different object at any time during the program execution, allowing for flexibility in manipulating and accessing different objects.

    Rate this question:

  • 8. 

    Which of the following rules are applicable for overriding?1 The argument list must exactly match that of the overridden method. 2 The return type can be of any type. 3 The access level can be more restrictive than the overridden methods.

    • A.

      1,2

    • B.

      Which of the following rules are applicable for overriding? 1 The argument list must exactly match that of the overridden method. 2 The return type can be of any type. 3 The access level can be more restrictive than the overridden methods.

    • C.

      2,3

    • D.

      3

    • E.

      1

    Correct Answer
    C. 2,3
    Explanation
    The argument list must exactly match that of the overridden method is not applicable for overriding. The return type can be of any type and the access level can be more restrictive than the overridden method are the applicable rules for overriding.

    Rate this question:

  • 9. 

    Which of the following statements are not true about a constructor? 1 Can use any access modifier, including private. 2 The constructor name should match the name of the class. 3 Constructors must have a return type.

    • A.

      2,3

    • B.

      1

    • C.

      1,2

    • D.

      3

    Correct Answer
    B. 1
    Explanation
    Constructors are special methods in a class that are used to initialize objects of that class. They cannot have a return type, therefore statement 3 is not true. Additionally, the constructor name should indeed match the name of the class, so statement 2 is also not true. However, constructors can use any access modifier, including private, so statement 1 is true. Therefore, the correct answer is 1.

    Rate this question:

  • 10. 

    The _________ and _________ classes enable objects and values of primitive types to be written to and read from streams.

    • A.

      ObjectOutputStream, ObjectInputStream

    • B.

      PipedOutputStream, ObjectInputStream

    • C.

      PipedInputStream, ObjectOutputStream

    • D.

      PipedInputStream, PipedOutputStream

    Correct Answer
    B. PipedOutputStream, ObjectInputStream
    Explanation
    The PipedOutputStream and ObjectInputStream classes enable objects and values of primitive types to be written to and read from streams. The PipedOutputStream class is used to write data to a pipe, while the ObjectInputStream class is used to read serialized objects from an input stream. Together, these classes provide the functionality to transfer objects and primitive values between different parts of a program using streams.

    Rate this question:

  • 11. 

    Which of the following are true about I/O filters?

    • A.

      Filters are supported on input, but not on output.

    • B.

      Filters read from one stream and write to another

    • C.

      Filters are supported by the InputStream/OutputStream class hierarchy but not by the Reader/Writer class hierarchy.

    • D.

      A filter may alter data that is read from one stream and written to another.

    Correct Answer
    C. Filters are supported by the InputStream/OutputStream class hierarchy but not by the Reader/Writer class hierarchy.
    Explanation
    Filters are supported by the InputStream/OutputStream class hierarchy but not by the Reader/Writer class hierarchy. This means that filters can be applied to input streams and output streams, but not to reader and writer objects. Filters are used to alter data that is read from one stream and written to another.

    Rate this question:

  • 12. 

    Which of the following is True about the three java.lang classes String, StringBuilder, and StringBuffer? 1. All three classes have a length () method.2. Objects of type StringBuffer are thread-safe.3. The value of an instance of any of these three types can be modified through various methods in the API.

    • A.

      1

    • B.

      3

    • C.

      1,2

    • D.

      2,3

    Correct Answer
    B. 3
    Explanation
    The correct answer is 3. The explanation for this answer is that all three classes (String, StringBuilder, and StringBuffer) have a length() method, which allows you to retrieve the length of the string. Additionally, the value of an instance of any of these three types can be modified through various methods in the API. However, only objects of type StringBuffer are thread-safe, meaning they can be safely used in a multi-threaded environment without any issues.

    Rate this question:

  • 13. 

    Given a properly prepared String array containing five elements, what range of results could a proper invocation of Arrays.binarySearch () produce?

    • A.

      0 through 4

    • B.

      0 through 5

    • C.

      6 through 4

    • D.

      -6 through 5

    Correct Answer
    C. 6 through 4
  • 14. 

    Which of the following statements are true about a static nested class? 1 You must have a reference to an instance of the enclosing class in order to instantiate it. 2 It does not have access to non-static members of the enclosing class. 3 Its variables and methods must be static.

    • A.

      2

    • B.

      1

    • C.

      3

    • D.

      None of the above

    Correct Answer
    C. 3
    Explanation
    A static nested class is a nested class that is declared with the static keyword. This means that it can be accessed without creating an instance of the enclosing class. Therefore, statement 1 is false. However, since it is a nested class, it does have access to both static and non-static members of the enclosing class. Therefore, statement 2 is false. Finally, there is no requirement for the variables and methods of a static nested class to be static. Therefore, statement 3 is true.

    Rate this question:

  • 15. 

    Which of these classes defined in java.io and used for file-handling are abstract?

    • A.

      PrintStream, InputStream

    • B.

      FileInputStream, Reader

    • C.

      FileWriter, InputStrea

    • D.

      InputStream, Reader

    Correct Answer
    B. FileInputStream, Reader
    Explanation
    The correct answer is FileInputStream, Reader. This is because both FileInputStream and Reader are abstract classes defined in the java.io package and are used for file-handling. Abstract classes cannot be instantiated and are meant to be extended by subclasses.

    Rate this question:

  • 16. 

    Which of the following are true about the File class? 1) A file object can be used to change the current working directory. 2) A file object can be used to access the files in the current working directory. 3) When a file object is created, a corresponding directory or file is created in the local file system. 4) File objects are used to access files and directories on the local file system. 5) File objects can be garbage collected.

    • A.

      1,3,5

    • B.

      2,4,5

    • C.

      2,3,4

    • D.

      1,2,3

    Correct Answer
    B. 2,4,5
    Explanation
    The File class in Java is used to access files and directories on the local file system, so statement 4 is true. File objects can be used to access the files in the current working directory, making statement 2 true. File objects can also be garbage collected, which means they can be automatically cleaned up by the Java garbage collector, making statement 5 true. Therefore, the correct answer is 2, 4, 5.

    Rate this question:

  • 17. 

    Which method of the CharArrayWriter class copies and converts the output buffer to a String object?

    • A.

      WriteTo()

    • B.

      Size()

    • C.

      ToString()

    • D.

      Reset()

    Correct Answer
    C. ToString()
    Explanation
    The toString() method of the CharArrayWriter class copies and converts the output buffer to a String object. This method returns a string that contains the characters in the buffer.

    Rate this question:

  • 18. 

    Which of the following are methods of InputStream class? 1) read() 2) available() 3) close() 4) flush()5) write()

    • A.

      1,3,5

    • B.

      2,4,5

    • C.

      1,2,3

    • D.

      3,4,5

    Correct Answer
    B. 2,4,5
    Explanation
    The correct answer is 2,4,5. The methods available(), flush(), and write() are not methods of the InputStream class. The available() method is used to return the number of bytes that can be read from the input stream without blocking. The flush() method is used to flush the output stream and write() method is used to write bytes to the output stream.

    Rate this question:

  • 19. 

    You need to read in the lines of a large text file containing tens of megabytes of data. Which of the following would be most suitable for reading in such a file?

    • A.

      New BufferedReader(new InputStreamReader(new FileInputStream("file.name");

    • B.

      New FileInputStream("file.name")

    • C.

      New InputStreamReader(new FileInputStream("file.name")

    • D.

      New RandomAccessFile raf = new RandomAccessFile("myfile.txt","+rw");

    Correct Answer
    B. New FileInputStream("file.name")
    Explanation
    The correct answer is "new FileInputStream("file.name")". This option is the most suitable for reading in a large text file because it directly reads the file as a stream of bytes, without any additional buffering or conversions. The other options involve additional layers of buffering or conversion, which may not be necessary and could potentially slow down the reading process for a large file.

    Rate this question:

  • 20. 

    Which of the following statements are true? 1 The notifyAll () method must be called from a synchronized context. 2 To call wait (), an object must own the lock on the thread. 3 The notify () method is defined in class java.lang.Thread.

    • A.

      2

    • B.

      None of the above

    • C.

      3

    • D.

      1

    Correct Answer
    C. 3
    Explanation
    The correct answer is 3. The notify() method is not defined in class java.lang.Thread. The notify() method is actually defined in class java.lang.Object. The notify() method is used to wake up a single thread that is waiting on the object's monitor.

    Rate this question:

  • 21. 

    What is the result of code given below?public static synchronized void main(String[] args) throwsInterruptedException {Thread t = new Thread();t.start();System.out.print("X");t.wait(10000);System.out.print("Y");}

    • A.

      It prints X and exits.

    • B.

      It prints X and never exits.

    • C.

      An exception is thrown at runtime.

    • D.

      It prints XY and exits almost immediately

    Correct Answer
    B. It prints X and never exits.
    Explanation
    The code creates a new thread and starts it. Then, it prints "X" and calls the wait() method on the thread object, causing the main thread to wait for 10 seconds. However, since the wait() method must be called within a synchronized block, an exception is thrown at runtime. Therefore, the correct answer is that an exception is thrown at runtime.

    Rate this question:

  • 22. 

    Consider the following line of code: int []x=new int[25]; After execution, which of the following are true? 1 x[24] is 0 2 x[24] is undefined 3 x[25] is 0

    • A.

      2

    • B.

      None of the above

    • C.

      3

    • D.

      1

    Correct Answer
    B. None of the above
    Explanation
    After executing the line of code "int []x=new int[25];", x[24] is not 0 or undefined. Since arrays in Java are zero-indexed, the last element of the array would be at index 24. However, this line of code only initializes the array and does not assign any values to its elements. Therefore, the value of x[24] would be the default value for an int, which is 0. However, x[24] is not undefined because it has been allocated memory and has a default value of 0. Similarly, x[25] is not 0 because it is out of bounds of the array. Therefore, the correct answer is "None of the above."

    Rate this question:

  • 23. 

    After execution of the following code fragment, what are the values of the variables x, a and b? int x, a=6,b=7;x=a++ + b++;

    • A.

      Fsdfsd

    • B.

      Asd

    Correct Answer
    B. Asd
  • 24. 

    Which of the following may appear on the left-hand side of an instanceOf operator? 1. a reference 2. a class 3. an interface4. a variable of primitive type

    • A.

      1,2

    • B.

      1,3

    • C.

      1.4

    • D.

      2,3

    Correct Answer
    B. 1,3
    Explanation
    The instanceOf operator in Java is used to check whether an object is an instance of a particular class or interface. On the left-hand side of the instanceOf operator, we can have a reference to an object (option 1) or an interface (option 3). A reference can be used to check if an object is an instance of a specific class or interface, while an interface can be used to check if an object implements a specific interface. Therefore, options 1 and 3 are correct.

    Rate this question:

  • 25. 

    Consider the following class definitions: Which of the following are legitimate calls to construct instances of the Test class? public class Test extends Base{ public Test(int j){}public Test(int j, int k){super(j,k);}} 1. Test t = new Test(); 2. Test t = new Test(1); 3. Test t = new Test(1,2);

    • A.

      1,2

    • B.

      2,3

    • C.

      3

    • D.

      1,3

    Correct Answer
    B. 2,3
    Explanation
    The Test class has two constructors - one that takes an integer parameter and another that takes two integer parameters. Option 1 is not a legitimate call because it does not match any of the available constructors. Option 2 is a legitimate call because it matches the constructor that takes a single integer parameter. Option 3 is also a legitimate call because it matches the constructor that takes two integer parameters. Therefore, the correct answer is 2,3.

    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 22, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 13, 2009
    Quiz Created by
    Suhaana
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.