Choose Correct Answer Core Java

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 Suhaana
S
Suhaana
Community Contributor
Quizzes Created: 1 | Total Attempts: 705
| Attempts: 705 | Questions: 25
Please wait...
Question 1 / 25
0 %
0/100
Score 0/100
1. Which method of the CharArrayWriter class copies and converts the output buffer to a String object?

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.

Submit
Please wait...
About This Quiz
Core Java Quizzes & Trivia

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

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

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.

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

Explanation

not-available-via-ai

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

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.

Submit
5. Which of the following are valid declarations?

1 int $x; 2 int 123; 3 int _123;

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.

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

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.

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


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.

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

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.

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

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.

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

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.

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

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.

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

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.

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

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.

Submit
14. 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 here
7. }   1. static void doStuff(int... doArgs) { } 2. static void doStuff(int[] doArgs) { } 3. static void doStuff(int x, int... doArgs) { }

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.

Submit
15. 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 f

3,private void xyz(float f)

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

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

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

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


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.

Submit
18. What is the result of code given below?

public static synchronized void main(String[] args) throws
InterruptedException {
Thread t = new Thread();
t.start();
System.out.print("X");
t.wait(10000);
System.out.print("Y");
}

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.

Submit
19. Which of the following may appear on the left-hand side of an instanceOf operator?

1. a reference 2. a class 3. an interface 4. a variable of primitive type

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.

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

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.

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

Explanation

not-available-via-ai

Submit
22. Which of the following are methods of InputStream class?

1) read() 2) available() 3) close() 4) flush() 5) write()

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.

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

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.

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

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.

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

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.

Submit
View My Results

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

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
Cancel
  • All
    All (25)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Which method of the CharArrayWriter class copies and converts the...
What value does readLine() return when it has reached the end of the...
After execution of the following code fragment, what are the values of...
Consider the following class definitions: Which of the following are...
Which of the following are valid declarations? ...
Which of the following statements are true about reference variables? ...
Which of these classes defined in java.io and used for file-handling...
Which of the following are true about the File class? ...
Which of the following are true about I/O filters?
2, 31 An anonymous inner class may implement at most one interface.2...
Which of the following statements are true? ...
Which of the following statements are true about a static nested...
Which of the following statements are true? ...
Which line if inserted independently at line 6, will compile in the...
Which of the following may override a method whose signature is void...
Consider the following line of code: int []x=new int[25]; After...
You need to read in the lines of a large text file containing tens of...
What is the result of code given below?public static synchronized void...
Which of the following may appear on the left-hand side of an...
Which of the following rules are applicable for overriding?1 The...
Given a properly prepared String array containing five elements, what...
Which of the following are methods of InputStream class? ...
Which of the following is True about the three java.lang classes...
The _________ and _________ classes enable objects and values of...
Which of the following statements are not true about a constructor? ...
Alert!

Advertisement