1.
Consider the following code:
public class TestOne {
public static void main(String args[])
{
byte x = 3;
byte y = 5;
System.out.print((y%x) + ", ");
System.out.println(y == ((y/x) *x +(y%x)));
}
}
Which of the following gives the valid output for above?
Correct Answer
C. Prints: 2, true
Explanation
The code first calculates the remainder of y divided by x using the modulus operator (%), which is 2. Then it checks if y is equal to the result of ((y/x) * x + (y%x)), which simplifies to ((5/3) * 3 + 2), which further simplifies to (1 * 3 + 2), which equals 5. Since y is indeed equal to 5, the expression evaluates to true. Therefore, the output will be "2, true".
2.
Consider the following program:
class A extends Thread {
public A(Runnable r) {}
public void run() {
System.out.print("A");
} }
class B implements Runnable {
public void run(){
System.out.print("B");
} }
class C{
public static void main(String[] args) {
new A(new B()).start();
} }
What will be the output of the above program?
Correct Answer
D. Prints: A
Explanation
The program creates an instance of class A, passing an instance of class B as a parameter to its constructor. Class A extends Thread and overrides the run() method to print "A". Therefore, when the new A object is started, it will execute the run() method and print "A" as the output.
3.
Dd3)6) Consider the following code:
public class Code17 {
public static void main(String args[]) {
new Code17();
}
{ System.out.print("Planet "); }
{ System.out.print("Welcome "); }
}
Which of the following will be the valid output for the above code?
Correct Answer
C. Planet Welcome
Explanation
The valid output for the given code will be "Planet Welcome". This is because the code is creating an instance of the class Code17 in the main method. Before the main method, there are two initialization blocks that print "Planet" and "Welcome" respectively. When the instance is created, these initialization blocks are executed in the order they appear, resulting in the output "Planet Welcome".
4.
Which of the following are the valid ways of creating wrapper type objects?
(Choose 3)
Correct Answer(s)
A. Float f = new Float("45.67d");
C. Byte bite = new Byte("-128");
E. Boolean b = new Boolean("23.9");
5.
Which of the following are characteristics of Annotations?(Choose 2)
Correct Answer(s)
A. Annotations are not part of a program.
D. Annotations are data about program.
Explanation
Annotations are not part of a program because they are metadata that provide additional information about the program but do not affect the program's execution. They can be used to add descriptive data or instructions to the program. Annotations are data about the program because they can be used to store information about the program's structure, behavior, or usage. This data can be accessed at runtime using reflection or used by other tools for code analysis or generation.
6.
Consider the following program:
public class D extends Thread { public void run() {
System.out.println("Before start method");
this.stop();
System.out.println("After stop method");
}
public static void main(String[] args) {
D a = new D(); a.start();
} }
What will be the output of the above program?
Correct Answer
D. 'Before start method' only
Explanation
The correct answer is 'Before start method' only. This is because the stop() method is called immediately after the start() method, which causes the thread to stop before it even starts executing the run() method. Therefore, the only output that will be printed is 'Before start method'.
7.
Which of the following methods are defined in Object class? (Choose 3)
Correct Answer(s)
A. Equals(Object)
B. HashCode()
C. ToString()
Explanation
The methods equals(Object), hashCode(), and toString() are defined in the Object class. The equals(Object) method is used to compare two objects for equality, the hashCode() method returns the hash code value for an object, and the toString() method returns a string representation of an object. These three methods are fundamental methods in Java and are inherited by all classes in the Java language.
8.
Consider the following code snippet:
class TestString3 {
public static void main(String args[]) {
String s1 = "Hello";
StringBuffer sb = new StringBuffer(s1);
sb.reverse();
s1.concat(sb.toString());
System.out.println(s1 + sb + s1.length() + sb.length());
} }
What will be the output of the above code snippet?
Correct Answer
B. HelloolleH55
Explanation
The output of the code snippet will be "HelloolleH55". This is because the code first creates a StringBuffer object "sb" and reverses it using the reverse() method. Then, it concatenates the reversed "sb" with the original string "s1" using the concat() method. However, the result of the concatenation is not stored in any variable. Finally, it prints the concatenation of "s1" and "sb", followed by the lengths of both strings. Since "s1" is still "Hello" and "sb" is reversed to "olleH", the output is "HelloolleH55".
9.
Both TYPE_SCROLL_SENSITIVE and TYPE_SCROLL_INSENSITIVE types
ResultSets will make changes visible if they are closed and then reopened.
State True or False.
Correct Answer
A. True
Explanation
Both TYPE_SCROLL_SENSITIVE and TYPE_SCROLL_INSENSITIVE ResultSets will make changes visible if they are closed and then reopened. This means that any changes made to the underlying data source will be reflected in the ResultSet when it is closed and then reopened. Therefore, the statement "True" is correct.
10.
Consider the following code snippet:
import java.io.*;
public class IOCode1 {
public static void main(String args[]) throws IOException {
BufferedReader br1 = new BufferedReader( new InputStreamReader(System.in));
BufferedWriter br2 = new BufferedWriter( new OutputStreamWriter(System.out));
String line = null;
while( (line = br1.readLine()) != null )
{
br2.write(line);
br2.flush();
}
br1.close();
br2.close();
} }
What will be the output for the above code snippet?
Correct Answer
E. Reads the text from keyboard line by line and prints the same to the console on pressing ENTER key at the end of every line
Explanation
The code snippet uses BufferedReader to read input from the keyboard line by line using the readLine() method. It then uses BufferedWriter to write the input to the console using the write() method. The code also flushes the output using the flush() method. Therefore, the code reads the text from the keyboard line by line and prints the same to the console on pressing the ENTER key at the end of every line.
11.
Consider the following scenario:
You are writing a set of classes related to cooking and have created your own
exception hierarchy derived from java.lang.Exception as follows:
Exception
+-Bad TasteException
+-Bitter Exception
+-Sour Excpetion
BadTasteException is defined as an abstract class. You have a method eatMe
that may throw a BitterException or a SourException.
Which of the following method declarations are acceptable to the compiler?
(Choose 3)
Correct Answer(s)
B. Public void eatMe() throws BadTasteException
C. Public void eatMe() throws RuntimeException
D. Public void eatMe() throws BitterException, SourException
Explanation
The compiler will accept the method declarations that correctly handle the exceptions that may be thrown by the eatMe method.
- The declaration "public void eatMe() throws BadTasteException" is acceptable because it correctly handles the exception of type BadTasteException that may be thrown by the method.
- The declaration "public void eatMe() throws RuntimeException" is acceptable because RuntimeException is an unchecked exception and does not need to be declared in the method signature.
- The declaration "public void eatMe() throws BitterException, SourException" is acceptable because it correctly handles the exceptions of type BitterException and SourException that may be thrown by the method.
12.
Consider the following code:
class Alpha {
protected Beta b;
}
class Gamma extends Alpha { }
class Beta { }
Which of the following statement is True?
Correct Answer
D. Gamma has-a Beta and Gamma is-a AlpHa
13.
Which of the following flow control features does Java support? (Choose 2)
Correct Answer(s)
B. Labeled continue
E. Labeled break
Explanation
Java supports the flow control features of labeled continue and labeled break. Labeled continue allows the program to skip the current iteration of a labeled loop and move to the next iteration. Labeled break allows the program to exit a labeled loop or switch statement. These features provide flexibility and control over the flow of execution in Java programs.
14.
Consider the following code:
class A {
public A getMe() {
return this;
}
}
class B extends A {
public static void main(String args[]) {
A a = new B() {
public A getMe() {
return this;
} };
System.out.println(a.getClass().getSuperclass().getName());
} }
Which of the following will be the output of the above code snippet?
Correct Answer
C. B
Explanation
The output of the code snippet will be "Object". This is because the code creates an anonymous class that extends class B, and overrides the getMe() method. When the getMe() method is called on the object "a", it returns the reference to the anonymous class object. However, when we use getClass().getSuperclass().getName(), it returns the superclass of the object, which is class A. Finally, getName() returns the name of the class, which is "Object" for the superclass.
15.
Which of the following statements are true?(Choose 2)
Correct Answer(s)
B. An object is a collection of properties and methods
C. A package is a collection of classes
Explanation
The correct answer is that an object is a collection of properties and methods and a package is a collection of classes. In object-oriented programming, an object is an instance of a class that encapsulates data (properties) and behavior (methods). A package, on the other hand, is a way to organize and group related classes together. Therefore, it is true that a package is a collection of classes.
16.
Which of the following are true about inheritance?(Choose 3)
Correct Answer(s)
A. In an inheritance hierarchy, a superclass can also act as a sub
class
C. In an inheritance hierarchy, a subclass can also act as a super
class
E. Inheritance enables adding new features and functionality to
an existing class without modifying the existing class
Explanation
Inheritance allows for the creation of a hierarchy where a superclass can also act as a subclass, meaning that it can inherit properties and methods from a higher-level class. Similarly, a subclass can also act as a superclass, meaning that it can have its own subclasses that inherit from it. This enables the sharing of data and methods among multiple classes, contradicting the statement that inheritance does not allow sharing. Additionally, inheritance allows for the addition of new features and functionality to an existing class without modifying the existing class, making it a powerful tool for extending and enhancing code.
17.
What of the following is the default Scroll type for a ResultSet object?
Correct Answer
B. ResultSet.TYPE_FORWARD_ONLY
Explanation
The default Scroll type for a ResultSet object is ResultSet.TYPE_FORWARD_ONLY. This means that the ResultSet can only be traversed in a forward direction and does not support scrolling or moving backwards.
18.
Consider the following code:
public class SwitchCase {
public static void main(String args[]) {
int x = 10; switch(x) {
case 10: System.out.println("10");
case 10: System.out.println("10");
case 20: System.out.println("20");
default: System.out.println("30");
} } }
Which of the following will be the output for the above program?
Correct Answer
C. Compilation Error
Explanation
The code will result in a compilation error because there are duplicate case labels in the switch statement. The case label "10" appears twice, which is not allowed in Java. Each case label in a switch statement must be unique.
19.
Consider the following code segment:
public class ExampleTwo {
public static void main(String args[]) {
int z = 8;
z += --z;
System.out.println("Value of z : " + z);
}
}
Which of the following gives the valid output for above?
Correct Answer
D. Prints: "Value of z: 15"
Explanation
The code segment starts with initializing the variable z to 8. Then, the expression "z += --z" is evaluated. The "--z" decrements the value of z by 1, resulting in z becoming 7. The "+=" operator adds the value of z (7) to the current value of z (7), resulting in z becoming 14. Finally, the value of z (14) is printed as part of the output statement "Value of z: 14". Therefore, the valid output for the code segment is "Value of z: 14".
20.
Which of the following exhibits the different ways of constructing a String
object?(Choose 3)
Correct Answer(s)
B. String s = new String("");
C. String can be constructed from byte arrays
E. String can be made from character arrays
Explanation
The correct answer choices demonstrate three different ways of constructing a String object. First, creating a String using an empty string literal: String s = new String("");. Second, constructing a String from byte arrays: String can be constructed from byte arrays. Lastly, creating a String from character arrays: String can be made from character arrays.
21.
Consider the following code snippet:
String a = "abc";
For the expression a="\""+a+"\""
What will be the output of the above code snippet?
Correct Answer
B. "abc"
Explanation
The code snippet concatenates the string "abc" with double quotes at the beginning and end. The backslash before each double quote is an escape character that allows the double quote to be included in the string. Therefore, the output of the code snippet will be "abc".
22.
Which of the following annotations are NOT defined in java.lang.annotation
package? (Choose 2)
Correct Answer(s)
A. @Deprecated
C. @SuppressWarnings
D. @Override
Explanation
The annotations @Deprecated, @SuppressWarnings, and @Override are not defined in the java.lang.annotation package. These annotations are part of the java.lang package, but not specifically defined in the java.lang.annotation package.
23.
What can cause a Thread to become non-runnable?
Correct Answer
A. Calling the wait method on an object.
Explanation
Calling the wait method on an object can cause a Thread to become non-runnable. The wait method is used to make a thread wait until another thread notifies it to resume. When a thread calls the wait method, it releases the lock on the object it is synchronized on and enters a waiting state. This means that the thread cannot continue its execution until it is notified by another thread. Therefore, calling the wait method on an object can cause a thread to become non-runnable.
24.
Which of the statements are True?
Correct Answer(s)
B. A constructor can be declared as private
D. A constructor can access the non-static member of a class
Explanation
A constructor can be declared as private means that a class can have a private constructor, which can only be accessed within the class itself. This is often used to restrict the creation of objects of that class to specific methods or within the class itself.
A constructor can access the non-static member of a class means that a constructor can access and initialize the non-static variables or members of a class. This allows the constructor to set initial values for the variables when an object is created.
25.
Consider the following line of Code:
int x[]=new int[25];
After execution ,which of the following statement/ statements are true?
Correct Answer(s)
B. X[24] is 0
E. X.length is 25
Explanation
The given line of code initializes an array of integers called "x" with a size of 25. In Java, when an array of integers is created, all elements are automatically initialized to their default value, which is 0 for integers. Therefore, x[24] is 0. Additionally, the length property of an array in Java returns the number of elements in the array, so x.length is 25. The other statements are incorrect.
26.
Which of the following are methods static members of the Thread class? (Choose all that apply)
Correct Answer(s)
B. Sleep
E. Yield
Explanation
The correct answer is sleep and yield. The sleep method is a static method in the Thread class that allows a thread to pause for a specified amount of time. The yield method is also a static method in the Thread class that allows a thread to voluntarily give up its current time slice and allow other threads to execute. The start, run, and wait methods are not static methods in the Thread class.
27.
A)Entries are not organised in key/value pairs.
b)Duplicate entries are rejected
Which interface of the java.util package offers the above specified behaviour?
Correct Answer
B. Set
Explanation
The correct answer is Set. The Set interface in the java.util package does not organize entries in key/value pairs and it rejects duplicate entries. This means that each element in a Set must be unique and there is no specific order maintained for the elements.
28.
Which of these belong to the set of Java keywords?
Correct Answer
E. None of the listed options
Explanation
None of the listed options belong to the set of Java keywords. Java keywords are predefined reserved words that have specific meanings and cannot be used as identifiers or variable names. The given options "statement", "validate", "assertion", and "exception" are not Java keywords.
29.
Consider the following code snippet:
class Sample{
public static void main(String[] args){
for(int i=0;i<5;i++){
switch(i)
case 0: System.out.println("v");break;
case 0: System.out.println("w");
case 0: System.out.println("x");break;
case 0: System.out.println("y");
case 0: System.out.println("z");break;
case 0: System.out.println("d");
}}}}
What is the result of attempting to compile and run the program?
Correct Answer
C. Prints: v w x x y z z
Explanation
The correct answer is "Prints: v w x x y z z". The code snippet contains a switch statement with multiple cases labeled as 0. Since all the cases have the same label, the code execution will fall through each case until it reaches a break statement. Therefore, when the variable i is 0, the code will print "v", "w", "x", and "z" because there are no break statements after these cases. As a result, the program will print "v w x x y z z".
30.
Which of the following lines will compile without warning or error?
Correct Answer
E. Int i=10;
Explanation
The line "int i=10;" will compile without warning or error because it is assigning a valid integer value (10) to the integer variable "i".