1.
Which of the following are true about ResultSet? (Choose 2)
Correct Answer(s)
B. Not all ResultSets are updatable
D. It is possible to delete records through ResultSet
Explanation
Not all ResultSets are updatable because some ResultSets may be read-only and cannot be modified. It is possible to delete records through ResultSet because the ResultSet object provides methods for deleting rows from the underlying table.
2.
Consider the following code:
import java.util.*;
public class Code10 {
{
final Vector v;
v=new Vector();
}
public Code10() { }
public void codeMethod() {
System.out.println(v.isEmpty());
}
public static void main(String args[]) {
new Code10().codeMethod();
}
}
Which of the following will be the output for the above code?
Correct Answer
C. Compilation error: cannot find the symbol
Explanation
The code will produce a compilation error: cannot find the symbol. This is because the variable "v" is declared and initialized within the instance initializer block, which is executed before the constructor. Therefore, the variable "v" is not accessible within the constructor or any other methods in the class. As a result, when the code tries to access "v" in the codeMethod() method, it cannot find the symbol and a compilation error occurs.
3.
Consider the following scenario:
Mr.Vijay is working for a Software Company. He needs to save and reload
objects from a Java application. He needs to write a module to accomplish the
same.
Which of the following options can be used to accomplish the above
requirement?
Correct Answer
E. Serializable interface
Explanation
The Serializable interface can be used to accomplish the requirement of saving and reloading objects from a Java application. This interface allows objects to be converted into a byte stream, which can then be saved to a file or transferred over a network. The byte stream can also be deserialized back into an object, allowing it to be reloaded. This makes the Serializable interface a suitable choice for Mr. Vijay to implement in his module.
4.
class InOut{
String s= new String("Between");
public void amethod(final int iArgs){
int iam;
class Bicycle{
public void sayHello(){
...Line 1
}
}//End of bicycle class
}//End of amethod
public void another(){
int iOther;
}
}
Which of the following statements would be correct to be coded at ...Line 1?
(Choose 2)
Correct Answer(s)
C. System.out.println(iArgs);
D. System.out.println(s);
5.
Which of the following modifiers cannot be used with the abstract modifier in a
method declaration?(Choose 3)
Correct Answer(s)
A. Synchronized
B. Final
E. Private
Explanation
The abstract modifier is used to indicate that a method does not have an implementation and must be overridden in a subclass. The synchronized modifier is used to ensure that only one thread can access a method at a time. Since an abstract method cannot have an implementation, it does not make sense to synchronize it. The final modifier is used to indicate that a method cannot be overridden, but abstract methods are meant to be overridden, so the final modifier cannot be used with the abstract modifier. The private modifier restricts access to a method, but abstract methods are meant to be accessed by subclasses, so the private modifier cannot be used with the abstract modifier.
6.
Consider the following code:
public class LabeledBreak2 {
public static void main(String args[]) {
loop:
for(int j=0; j<2; j++) {
for(int i=0; i<10; i++) {
if(i == 5) break loop;
System.out.print(i + " ");
}
}
}
}
Which of the following will be the output for the above code?
Correct Answer
D. 0 1 2 3 4
Explanation
The code contains a nested loop. The outer loop runs two times and the inner loop runs ten times. Inside the inner loop, there is a condition that checks if the value of 'i' is equal to 5. If it is, the 'break loop' statement is executed, which breaks out of both the inner and outer loops. Therefore, when 'i' reaches 5, the loop is terminated and the program prints the numbers 0 to 4. Hence, the output of the code will be "0 1 2 3 4".
7.
Consider the following code:
public class Key1 {
public boolean testAns( String ans, int n ) {
boolean rslt;
if (ans.equalsIgnoreCase("YES") & n > 5)
rslt = true;
return rslt;
}
public static void main(String args[]) {
System.out.println(new Key1().testAns("no", 5));
}
}
Which of the following will be the output of the above program?
Correct Answer
C. Compile-time error
Explanation
The code will result in a compile-time error because the variable "rslt" is not initialized before it is returned.
8.
Which of the following is the immediate super interface of CallableStatement?
Correct Answer
B. PreparedStatement
Explanation
The immediate super interface of CallableStatement is PreparedStatement. This is because CallableStatement is a subinterface of PreparedStatement, which is a subinterface of Statement. Therefore, PreparedStatement is the direct parent interface of CallableStatement.
9.
Consider the following code:
public class UnwiseThreads implements Runnable {
public void run() {
while(true) { }
}
public static void main(String args[]) {
UnwiseThreads ut1 = new UnwiseThreads();
UnwiseThreads ut2 = new UnwiseThreads();
UnwiseThreads ut3 = new UnwiseThreads();
ut1.run();
ut2.run();
ut3.run();
}
}
Which of the following is correct for the above given program?
Correct Answer
C. The code compiles but runs only 1 non ending, non daemon thread
Explanation
The code compiles without any errors. In the main method, three instances of the UnwiseThreads class are created. However, instead of starting the threads using the start() method, the run() method is called directly on each instance. This means that the code runs sequentially and does not create separate threads. Therefore, only one thread is executed, resulting in the code running only 1 non-ending, non-daemon thread.
10.
Which of the following is the best-performing implementation of Set interface?
Correct Answer
E. HashSet
Explanation
HashSet is the best-performing implementation of the Set interface because it offers constant time performance for basic operations such as add, remove, and contains. It achieves this by using a hash table to store elements, which allows for efficient lookup and retrieval. HashSet does not maintain any specific order of elements, making it faster compared to other implementations like LinkedHashSet or TreeSet, which have additional overhead for maintaining insertion order or sorting elements. Hashtable is not a recommended implementation for Set interface as it is a synchronized class, which can impact performance in multi-threaded environments. SortedSet is an interface, not an implementation, so it cannot be considered as the best-performing implementation.
11.
12
Consider the following scenario:
Real Chocos Private Limited deals in manufacturing variety of chocolates.
This organization manufactures three varieties of chocolates.
1. Fruit Chocolates
2. Rum Chocolates
3. Milk Chocolates
A software system needs to be built.
Which of the following options identifies the Classes and Objects?
Correct Answer
B. Class: Chocolate Objects: Fruit Chocolates, Rum Chocolates, Milk Chocolates
Explanation
In this scenario, the correct answer is "Class: Chocolate Objects: Fruit Chocolates, Rum Chocolates, Milk Chocolates." This is because the class represents the general category of chocolates, while the objects represent the specific varieties of chocolates that the organization manufactures.
12.
Consider the following code snippet:
class Animal {
String name;
public boolean equals(Object o) {
Animal a = (Animal) o;
// Code Here
}
}
class TestAnimal {
public static void main(String args[]) {
Animal a = new Animal();
a.name = "Dog";
Animal b = new Animal();
b.name = "dog";
System.out.println(a.equals(b));
}
}
Which of the following code snippets should be replaced for the comment line
(//Code Here) in the above given code, to get the output as true?
Correct Answer
A. Return this.name.equalsIgnoreCase(a.name);
Explanation
The correct answer is "return this.name.equalsIgnoreCase(a.name);" because it compares the names of the two Animal objects while ignoring the case sensitivity. This will return true if the names are equal, regardless of whether they are in uppercase or lowercase.
13.
Consider the following code:
class A { }
class B extends A { }
public class Code2 {
public void method(A a) {
System.out.println("A");
}
public void method(B b) {
System.out.println("B");
}
public static void main(String args[]) {
new Code2().method(new Object());
}
}
Which of the following will be the output for the above code?
Correct Answer
C. Compilation Error 'Cannot find the symbol'
Explanation
The code will produce a compilation error 'Cannot find the symbol' because the method `method()` in the `Code2` class does not have an overload that accepts an `Object` parameter. The only available overloads are `method(A a)` and `method(B b)`, so when trying to call `method(new Object())`, the compiler cannot find a suitable method to match the argument type.
14.
Consider the following code:
class Planet { }
class Earth extends Planet { }
public class WelcomePlanet {
public static void welcomePlanet(Planet planet) {
if (planet instanceof Earth) {
System.out.println("Welcome!");
} else if (planet instanceof Planet) {
System.out.println("Planet!");
} else {
System.exit(0);
}
}
public static void main(String args[]) {
WelcomePlanet wp = new WelcomePlanet();
Planet planet = new Earth();
welcomePlanet(planet);
}
}
Which of the following will be the output of the above program?
Correct Answer
D. Welcome!
Explanation
The output of the above program will be "Welcome!". This is because the main method creates an instance of the WelcomePlanet class and assigns it to the variable wp. Then, a new instance of the Earth class is created and assigned to the variable planet. The welcomePlanet method is called with the planet variable as the argument. Inside the welcomePlanet method, the first if statement checks if the planet is an instance of Earth, which it is. Therefore, the code inside the if statement is executed and "Welcome!" is printed to the console.
15.
Consider the following code:
public class Code13 {
public static void main(String... args) {
for(String s:args)
System.out.print(s + ", ");
System.out.println(args.length);
}
}
Which of the following will be the output if the above code is attempted to
compile and execute?
Correct Answer
A. Program compiles successfully and prints the passed arguments as comma separated values and finally prints the length of the arguments-list
Explanation
The code will compile successfully and print the passed arguments as comma-separated values. It will also print the length of the arguments list. This is because the code uses the enhanced for-loop to iterate over the arguments array and prints each argument followed by a comma. Finally, it prints the length of the arguments array using the length property.
16.
Consider the following code:
class UT1 { static byte m1() { final char c = 'u0001'; return c;
}
static byte m3(final char c) {return c;}
public static void main(String[] args) { char c = 'u0003'; System.out.print(""+m1()+m3(c)); } }
Which of the following gives the valid output of the above code?
Correct Answer
A. Compile-time error
Explanation
The code will result in a compile-time error because the method m1() is declared to return a byte, but it is actually returning a char. Since a char cannot be automatically converted to a byte, the code will fail to compile.
17.
An Annotation Type ________________.
Correct Answer
E. Defines the structure of an Annotation
Explanation
An annotation type is used to define the structure of an annotation. Annotations are used to provide additional information about code elements, such as classes, methods, or fields. They can be used by the code or by the JVM to perform specific actions or make certain decisions at runtime. By defining the structure of an annotation using an annotation type, developers can specify what elements can be included in the annotation and what values they can have. This allows for more flexibility and control when using annotations in code.
18.
Which of the following are correct regarding HashCode?(Choose 2)
Correct Answer(s)
A. It improves performance
C. It is a 32 bit numeric digest key
Explanation
The first correct statement is that HashCode improves performance. This is because HashCode is used in hashing algorithms to quickly locate objects in data structures like hash tables. By providing a unique numeric representation for each object, HashCode allows for efficient retrieval and storage of data.
The second correct statement is that HashCode is a 32-bit numeric digest key. This means that the HashCode value is a 32-bit integer that represents a digest or summary of the object's data. This allows for efficient comparison and identification of objects based on their HashCode values.
19.
Given the following object hierarchy and code for the upgrade method:
java.lang.Object
+----mypkg.BaseWidget
|
+----TypeAWidget
// the following is a method in the BaseWidget class
1. public TypeAWidget upgrade( ){
2. TypeAWidget A = (TypeAWidget) this;
3. return A;
4. }
Which of the following will be the result of the below statements?
5. BaseWidget B = new BaseWidget();
6. TypeAWidget A = B.upgrade();
Correct Answer
B. A runtime ClassCastException would be generated in line 2.
Explanation
The code is trying to cast an object of type BaseWidget to TypeAWidget using the "upgrade" method. However, since the object B is an instance of BaseWidget and not TypeAWidget, a ClassCastException will be thrown at line 2, indicating that the casting is not possible.
20.
Consider the following program:
public class ThreadJoin extends Thread{
public static void main(String[] args) {
Thread t1 = new Thread("T1");
Thread t2 = new Thread("T2");
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
System.out.println("Main Thread interrupted.");
}
}
public void run(){
System.out.println("Run executed");
}
}
What will be the output of the above program?
Correct Answer
D. Program ends without printing anything
Explanation
The program ends without printing anything because the main thread is calling the join() method on t1 and t2 threads without starting them. The join() method waits for the completion of the specified thread, but since the threads are not started, they never execute the run() method and the program ends without printing anything.
21.
Which are all platform independent among the following? (Choose 3)
Correct Answer(s)
A. JAR Files
D. Java Class Files
E. Java Source Files
Explanation
JAR Files, Java Class Files, and Java Source Files are all platform independent. JAR Files are archives that contain compiled Java classes and resources, which can be executed on any platform that has a Java Virtual Machine (JVM). Java Class Files are compiled bytecode that can be executed on any platform with a JVM. Java Source Files are plain text files that contain Java code and can be compiled into Java Class Files, which are then executed on a JVM. Therefore, all three options can be run on multiple platforms without any modifications.
22.
Which of the following options is true about multiple inheritance?
Correct Answer
B. Inheriting from more than one super class
Explanation
Multiple inheritance refers to the ability of a class to inherit attributes and behaviors from more than one superclass. In this case, the correct answer states that multiple inheritance involves inheriting from more than one super class. This means that a subclass can inherit characteristics from two or more different parent classes, allowing it to access and utilize the attributes and methods defined in each of those super classes.
23.
Which of the following options give the names of data structures that can be
used for elements that have ordering, but no duplicates? (Choose 2)
Correct Answer(s)
B. SortedSet
E. TreeSet
Explanation
SortedSet and TreeSet are data structures that can be used for elements that have ordering but no duplicates. SortedSet is an interface in Java that extends the Set interface and maintains the elements in a sorted order. It does not allow duplicates. TreeSet is a class in Java that implements the SortedSet interface and internally uses a balanced tree structure to store the elements. It also maintains the elements in a sorted order and does not allow duplicates.
24.
Which of the following options are true for StringBuffer class?(choose 3)
Correct Answer(s)
C. StringBuffer implements Charsequence interface
D. StringBuffer is threadsafe
E. Buffer space in StringBuffer can be shared
Explanation
The given answer is correct.
1) StringBuffer implements the Charsequence interface, which means it can be used to represent a sequence of characters.
2) StringBuffer is thread-safe, which means multiple threads can safely access and modify a StringBuffer object without causing any data corruption or inconsistency.
3) The buffer space in a StringBuffer can be shared, allowing multiple strings to be appended or inserted into the same buffer without creating new objects. This can improve performance and memory efficiency.
25.
Consider the following partial code:
public class CreditCard {
private String cardID;
private Integer limit;
public String ownerName;
public void setCardInformation(String cardID, String ownerName, Integer limit) {
this.cardID = cardID;
this.ownerName = ownerName;
this.limit = limit;
}
}
Which of the following statement is True regarding the above given code?
Correct Answer
E. The ownerName variable breaks encapsulation
Explanation
The ownerName variable breaks encapsulation because it is declared as public, allowing direct access to it from outside the class. Encapsulation refers to the practice of hiding internal data and implementation details of a class, and providing access to them through methods. By declaring the ownerName variable as public, it can be accessed and modified directly without going through any methods, which violates the principle of encapsulation.
26.
Consider the following partial code:
interface A { public int getValue(); }
class B implements A {
public int getValue() { return 1; }
}
class C extends B {
// insert code here
}
Which of the following code fragments, when inserted individually at the
commented line (// insert code here), makes use of polymorphism? (Choose 3)
Correct Answer(s)
B. Public void add(A a, B b) { a.getValue(); }
C. Public void add(B b) { b.getValue(); }
D. Public void add(A a) { a.getValue(); }
Explanation
The correct answer is "public void add(A a, B b) { a.getValue(); }, public void add(B b) { b.getValue(); }, public void add(A a) { a.getValue(); }". These code fragments make use of polymorphism because they accept objects of different classes (A, B, and C) as parameters, but they all call the getValue() method, which is defined in the interface A and implemented in the class B. This demonstrates polymorphism, as the same method can be called on objects of different classes that implement the same interface.
27.
Consider the following program:
import java.io.*;
public class CrypticCatch {
public static void main(String[] args) throws Exception {
try {
try {
try {
throw new FileNotFoundException();
} catch(Exception e3) {
throw e3;
}
} catch(IOException e2) {
throw e2;
}
} catch(FileNotFoundException e1) {
System.out.println("File not found exception caught");
}
System.out.println("Exception handled successfully");
}
}
What will be the output of the above program?
Correct Answer
E. File not found exception caughtException handled successfully
Explanation
The output of the program will be "File not found exception caught" followed by "Exception handled successfully". This is because the program throws a FileNotFoundException in the innermost try block, which is then caught by the catch block for FileNotFoundException. The catch block prints "File not found exception caught". After that, the program continues to execute and prints "Exception handled successfully".
28.
Which of the following annotations are defined in java.lang.annotation
package? (Choose 2)
Correct Answer(s)
A. @Retention
E. @Target
Explanation
The annotations @Retention and @Target are defined in the java.lang.annotation package. The @Retention annotation is used to specify how long an annotation should be retained, whether it should be available at runtime or only during compilation. The @Target annotation is used to specify the elements to which an annotation can be applied, such as classes, methods, or fields. Both of these annotations are commonly used in Java programming to provide additional information or metadata about code elements.
29.
What are the new updations to java.io.File class in JDK 1.6?(Choose 2)
Correct Answer(s)
A. Methods to retrieve disk usage information
B. Methods to set or query file permissions
Explanation
In JDK 1.6, two new updations were made to the java.io.File class. The first updation is the addition of methods to retrieve disk usage information. These methods allow the user to obtain information about the amount of disk space used by a file. The second updation is the addition of methods to set or query file permissions. These methods enable the user to manipulate and check the permissions associated with a file. No new methods were introduced in JDK 1.6 for attaching the file to an email or encrypting the file with a password.
30.
Consider the following code:
1. public class DagRag {
2. public static void main(String [] args) {
3.
4. int [][] x = new int[2][4];
5.
6. for(int y = 0; y < 2; y++) {
7. for(int z = 0; z < 4; z++) {
8. x[y][z] = z;
9. }
10. }
11.
12. dg: for(int g = 0; g < 2; g++) {
13. rg: for(int h = 0; h < 4; h++) {
14. System.out.println(x[g][h]);
15.
16. }
17. System.out.println("The end.");
18.
19. }
20.
21. }
22. }
Which of the following code snippet when inserted at lines 15 and 18 respectively, will make
the above program to generate the below output?
0
1
2
3
The end.
Correct Answer
C. If(h==3) break rg;if(g==0) break dg;
31.
Consider the following code:
public abstract class Shape {
private int x;
private int y;
public abstract void draw();
public void setAnchor(int x, int y) {
this.x = x;
this.y = y;
}
}
Which of the following implementations use the Shape class correctly? (Choose
2)
Correct Answer(s)
A. Public class Circle extends Shape {private int radius;public void setRadius(int radius) { this.radius = radius; }public int getRadius() { return radius; }public void draw() {/* code here */}}
D. Public abstract class Circle extends Shape {private int radius;}
Explanation
The first implementation correctly extends the Shape class and provides the necessary implementation for the draw() method. It also includes methods to set and get the radius of the Circle.
The second implementation also correctly extends the Shape class but does not provide any additional implementation or methods specific to the Circle class. However, it still follows the correct structure and inheritance.
Therefore, the first and second implementations use the Shape class correctly.
32.
Consider the following code snippet:
public class TestString9 {
public static void main(String st[]){
String s1 = "java";
String s2 = "java";
String s3 = "JAVA";
s2.toUpperCase();
s3.toUpperCase();
boolean b1 = s1==s2;
boolean b2 = s1==s3;
System.out.print(b1);
System.out.print(" "+b2);
}
}
What will be the output of the above code snippet?
Correct Answer
C. True false
Explanation
The output of the above code snippet will be "true false". This is because the variables s2 and s3 are not actually modified by the toUpperCase() method. In Java, strings are immutable, meaning that their values cannot be changed once they are created. So when the toUpperCase() method is called on s2 and s3, it creates new strings with the uppercase versions of the original strings, but these new strings are not assigned to any variable. Therefore, the comparison of s1 with s2 will be true because they both refer to the same string "java", while the comparison of s1 with s3 will be false because they refer to different strings ("java" and "JAVA").
33.
Consider the following code snippet:
import java.io.*;
public class IOCode2 {
public static void main(String args[]) throws FileNotFoundException
{
// Insert Code here
System.out.println("Welcome to File Programming");
}
}
Which of the following code snippets when substituted to the comment line (// Insert Code here), will redirect the output generated by the System.out.println() methods, in the above code?
Correct Answer
D. System.setOut(new PrintStream("C:/Data"));
Explanation
The correct answer is System.setOut(new PrintStream("C:/Data")); This code snippet redirects the output generated by the System.out.println() method to the file "C:/Data" by setting the output stream to a new PrintStream object that is created with the file as its parameter.
34.
Which of the following types of driver provides maximum decoupling between
database and Java application?
Correct Answer
B. Type III driver
Explanation
A Type III driver provides maximum decoupling between the database and Java application. This is because it uses a middleware server between the database and the application, allowing for easy migration between different databases without changing the application code. The middleware server translates the JDBC calls from the application into the specific database protocol, providing a high level of abstraction and flexibility. This driver is also known as the Network Protocol driver and is commonly used in web-based applications.
35.
Consider the following code snippet:
import java.util.*;
class Student {
String studentName; Student() { }
Student(String studentName)
{
this.studentName = studentName;
}
public String toString() { return this.studentName; }
}
public class TestCol7
{
public static void main(String args[])
{
TreeSet students = new TreeSet();
students.add(new Student("Raju"));
students.add(new Student("Krishna"));
students.add(new Student("Vijay"));
System.out.println(students);
}
}
Running the above code, throws Runtime exception.
Which of the following options will make the code run properly?
Correct Answer
A. The Student class should implement Comparable interface.
Explanation
The code snippet is using a TreeSet to store objects of the Student class. A TreeSet requires its elements to be comparable in order to maintain a sorted order. Therefore, the Student class should implement the Comparable interface and override the compareTo() method to define the natural ordering of Student objects based on their names. This will allow the TreeSet to properly sort and store the Student objects without throwing a runtime exception.
36.
Consider the following code snippet:
1. class Garbage { }
2. class GC1 {
3. public static void main(String a[]) {
4. Garbage s = new Garbage();
5. {
6. s = new Garbage();
7. }
8. s = new Garbage();
9. }
10. }
Which of the following options gives the correct combination of lines that makes objects eligible for garbage Collection?
Correct Answer
D. Lines: 6, 8
Explanation
In the given code snippet, an object of the class Garbage is created and assigned to the variable 's' at line 4. Then, a new object of Garbage is created and assigned to 's' within a block at line 6. This means that the object created at line 4 is no longer referenced and becomes eligible for garbage collection. Finally, at line 8, a new object of Garbage is created and assigned to 's' again, making the object created at line 6 also eligible for garbage collection. Therefore, the correct combination of lines that makes objects eligible for garbage collection is lines 6 and 8.
37.
Which of the following options are true? (Choose 2)
Correct Answer(s)
A. The catch block can have another try-catch-finally block
D. The finally block can have another try-catch-finally blocknested inside
Explanation
The catch block can have another try-catch-finally block because it is possible to handle exceptions within the catch block by placing another try-catch-finally structure inside it. This allows for further error handling and exception management. Similarly, the finally block can have another try-catch-finally block nested inside it, allowing for additional error handling and exception management within the finally block.
38.
Consider the following program:
public class TThread implements Runnable {
public void run() {
try {
Thread.sleep(100000);
} catch (Exception objE) {
System.out.println ("Exception Handler");
}
System.out.println ("Run method ends here");
}
public static void main (String[] argv) {
Thread thread = new Thread(new TThread ());
thread.start();
thread.interrupt();
System.out.println ("Main method ends here");
}
}
What will be the output of the above program?
Correct Answer
C. Run method ends here Exception Handler Main method ends here
Explanation
The program creates a new thread and starts it. The new thread sleeps for 100000 milliseconds (100 seconds) and then prints "Run method ends here". Meanwhile, the main thread interrupts the new thread and prints "Main method ends here". Since the new thread is interrupted, it throws an InterruptedException and the catch block is executed, printing "Exception Handler". Therefore, the correct output is "Run method ends here Exception Handler Main method ends here".
39.
Consider the following code:
1. class Test {
2. public static void main(String args[]) {
3. double d = 12.3;
4. Dec dec = new Dec();
5. dec.dec(d);
6. System.out.println(d);
7. }
8. }
9. class Dec{
10. public void dec(double d) { d = d - 2.0d; }
11. } Which of the following gives the correct value printed at line 6?
Correct Answer
A. Prints: 12.3
Explanation
The code creates an object of the Dec class and calls the dec() method on that object, passing in the value of the variable "d". Inside the dec() method, the value of "d" is subtracted by 2.0d, but this does not affect the value of the variable "d" in the main method. Therefore, when the value of "d" is printed at line 6, it is still the original value of 12.3.
40.
Consider the following Statements:
Statement A:The threads are scheduled using fixed priority scheduling.
Statement B:Thread priority can be set after it is created using the public int setPriority() method declared in the Thread class. Which of the following statements is correct?
Correct Answer
C. Statement A is true and Statement B is false
Explanation
In this question, the correct answer is "Statement A is true and Statement B is false". This means that the threads are scheduled using fixed priority scheduling (Statement A is true), but the thread priority cannot be set after it is created using the public int setPriority() method declared in the Thread class (Statement B is false).
41.
Consider the following code snippet:
import java.util.*;
public class TestCol4 {
public static void main(String[] args) {
Set h = new HashSet();
h.add("One");
h.add("Two");
h.add("Three");
h.add("Four");
h.add("One");
h.add("Four");
List l = new ArrayList();
l.add("One");
l.add("Two");
l.add("Three");
h.retainAll(l);
System.out.println("Size:" + l.size() + h.size());
}
}
What will be the output of the above code snippet?
Correct Answer
A. Size: 63
Explanation
The output of the code snippet will be "Size: 63". The code snippet first creates a HashSet object "h" and adds six string elements to it. Then, it creates an ArrayList object "l" and adds three string elements to it. The "retainAll" method is called on the HashSet object "h" with the ArrayList object "l" as the argument. This method retains only the elements in "h" that are also present in "l". After that, the code snippet prints the size of "l" (which is 3) concatenated with the size of "h" (which is 6), resulting in "Size: 63".
42.
Consider the following program:
1. class CheckedException extends RuntimeException { }
2. class UncheckedException extends Exception { }
3. public class Check {
4. public static void main(String args[]) {
5. generateException1();
6. generateException2();
7. }
8.
9. private static void generateException1() {
10. throw new CheckedException();
11. }
12.
13. private static void generateException2() {
14. throw new UncheckedException();
15. }
16. }
Which of the following is true regarding the above given program?Compilation error at line 6
Correct Answer
C. Compilation error at line 14
Explanation
The given program will result in a compilation error at line 14. This is because the method generateException2() is declared to throw an UncheckedException, which is a checked exception. However, the method does not have a try-catch block to handle the exception or a throws clause in its method signature. Therefore, the compiler will throw a compilation error at line 14.
43.
Consider the following partial code:
class Bean {
interface I
{ void beanInterface(); }
class BeanI extends Bean implements I { }
}
public class BeanImpl {
public static void main(String args[]) {
Bean bean = new Bean();
Bean.BeanI beanI = bean. new BeanI();
beanI.beanInterface();
}
}
Which of the following changes made to the class Bean without changing the class BeanImpl, will make the above code to compile properly?
Correct Answer
D. Add the following method to Bean class public void beanInterface() { }
Explanation
Adding the method "public void beanInterface() { }" to the Bean class will make the code compile properly because the BeanI class implements the I interface, which requires the implementation of the beanInterface() method. By adding this method to the Bean class, the BeanI class will have access to the implementation of the method and the code will compile without any errors.
44.
Consider the following program:
class UserDefinedException extends Error { }
public class TasteIt {
public static void main(String args[])
{
try
{
try
{
throw new Error();
}
catch(UserDefinedException u1)
{ throw u1; }
catch(Exception e1)
{ System.out.println("This is the required output"); }
finally
{ throw new UserDefinedException(); }
}
catch(UserDefinedException u2)
{ System.out.println("This is not the output"); }
catch(Error e2)
{ System.out.println("This is the output"); }
}
}
What will be the output for the above program?Runtime Error
Correct Answer
B. This is not the output
Explanation
The output for the above program will be "This is the output". This is because the program throws an Error in the inner try block, but it is caught by the catch block for Exception. Therefore, the catch block for UserDefinedException is not executed. Finally, a UserDefinedException is thrown, but it is caught by the catch block for Error, which prints "This is the output".