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)
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).
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.
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.
3.
Which of the following are valid declarations?
1 int $x;
2 int 123;
3 int _123;
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.
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) { }
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.
5.
What value does readLine() return when it has reached the end of the file?
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.
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.
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.
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.
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.
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.
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.
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.
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.
10.
The _________ and _________ classes enable objects and values of primitive types to be written to and read from streams.
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.
11.
Which of the following are true about I/O filters?
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.
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.
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.
13.
Given a properly prepared String array containing five elements, what range of results could a proper invocation of Arrays.binarySearch () produce?
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.
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.
15.
Which of these classes defined in java.io and used for file-handling are abstract?
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.
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.
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.
17.
Which method of the CharArrayWriter class copies and converts the output buffer to a String object?
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.
18.
Which of the following are methods of InputStream class?
1) read()
2) available()
3) close()
4) flush()5) write()
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.
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?
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.
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.
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.
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");}
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.
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
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."
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++;
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
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.
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);
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.