1.
Consider the following code:
Line no 1:class Outer{
Line no 2:public static class Inner{
Line no 3:}
Line no 4:public static void display(){ } }
Line no 5:public class Test
Line no 6:{
Line no 7:public static void main(String args[])
Line no 8:{
Line no 9://Replace with code from the option below
Line no 10:}}
Which of the following option when replaced at line no 9,instantiates an instance of the nested class?
Correct Answer
B. Outer.Inner o = new Outer.Inner();
Explanation
The correct answer is "Outer.Inner o = new Outer.Inner();". This statement creates an instance of the nested class Inner by using the syntax "Outer.Inner". The "new" keyword is used to create a new instance of the class, and the outer class "Outer" is specified before the nested class "Inner" to indicate the nesting relationship.
2.
Which of the following are correct regarding method overriding?(Choose 2)
Correct Answer(s)
A. Both the methods must not be in the same class
B. Same name same signature
Explanation
Method overriding is a feature in object-oriented programming where a subclass provides a specific implementation of a method that is already defined in its superclass. In order for method overriding to occur, both the methods (the one in the superclass and the one in the subclass) must have the same name and the same signature. However, it is important to note that the methods must not be in the same class. This is because method overriding is a way to provide a different implementation for a method in a subclass, and if both methods were in the same class, it would not be considered overriding but rather overloading.
3.
Which of the following statement is true regarding method overloading?
Correct Answer
D. Methods can be overloaded across inherited classes
Explanation
Methods can be overloaded across inherited classes means that a subclass can have its own version of an overloaded method that is already present in its superclass. This allows the subclass to provide a different implementation of the method while still maintaining the same name and parameter types. This feature enables polymorphism and allows for more flexibility and customization in the behavior of inherited classes.
4.
Runtime exception can be handled:
Correct Answer
A. True
Explanation
Runtime exceptions can be handled in a program. Unlike checked exceptions, which must be declared or caught, runtime exceptions do not require explicit handling. However, it is considered good practice to handle runtime exceptions to prevent the program from crashing or exhibiting unexpected behavior. This can be done by using try-catch blocks to catch and handle the specific runtime exceptions that may occur during the execution of the program.
5.
Unboxing the Numeric Wrapper types to primitive types is done under operations (choose 3)
Correct Answer(s)
A. ++
C. -
E. ==
Explanation
Unboxing is the process of converting an object of a wrapper class to its corresponding primitive type. In this case, unboxing the numeric wrapper types to primitive types can be done using the increment operator (++), the subtraction operator (-), and the equality operator (==). The increment operator is used to increase the value by 1, the subtraction operator is used to perform arithmetic subtraction, and the equality operator is used to compare two values for equality.
6.
Which of the following option is not a valid wrapper type object creation?
Correct Answer
A. New Boolean(“truth”);
Explanation
The option "new Boolean("truth");" is not a valid wrapper type object creation because the constructor for the Boolean class only accepts the values "true" or "false" as arguments. Using any other value, such as "truth", will result in a compilation error.
7.
What will be the output of following code?
Int a=2;
Integer b=2;
System.out.println(a==b);
Correct Answer
C. 1
Explanation
The code will compile without any errors. The output of the code will be 1. This is because the "==" operator is used to compare the values of the variables. In this case, the value of the variable "a" is 2 and the value of the variable "b" is also 2. Since both variables have the same value, the expression "a==b" will evaluate to true, which will be printed as 1.
8.
Which of the following correctly fits for the definition holding instances of other objects?
Correct Answer
B. Generic
Explanation
Generic correctly fits the definition of holding instances of other objects. In programming, generic refers to a type that can be used with different data types. It allows for the creation of classes, methods, or interfaces that can work with different types of objects, providing flexibility and reusability. With generics, we can create a single piece of code that can handle multiple types of objects, making it a suitable choice for holding instances of other objects.
9.
Consider the following code:
Class MyError extends Error{ }
Public class TestError {
Public static void main(String args[]) {
Try {
Test();
} catch(Error ie) {
System.out.println(“Error caught”);
}
}
Static void test() throws Error {
Throw new MyError();
}
}
Which of the following option gives the output for the above code?
Correct Answer
B. Compile time error
Cannot catch Error type objects
Explanation
The code will result in a compile time error because the catch block is trying to catch an instance of the Error class, which is not allowed. The Error class is a subclass of the Throwable class, and it is not intended to be caught by user code. Therefore, the code will not compile.
10.
Consider the following code:
01 import java.util.Set;
02 import java.util.TreeSet;
03
04 class TestSet{
05 public static void main(String[] args) {
06 Set set = new TreeSet();
07 set.add(“Green World”);
08 set.add(1);
09 set.add(“Green Peace”);
10 System.out.println(set);
11 }
12 }
Which of the following option gives the output for the above code?
Correct Answer
B. Throws Runtime Exception
Explanation
The code will throw a runtime exception because it is attempting to add different types of objects (a String and an Integer) to the Set, which is not allowed. The TreeSet class in Java requires all elements to be of the same type, so trying to add different types will result in a ClassCastException.
11.
Select the correct answer
Correct Answer
C. All the methods of an interface are by default abstract
Explanation
An interface is a collection of abstract methods that define a contract for classes to implement. By default, all the methods declared in an interface are abstract, meaning they do not have a body or implementation. This is because interfaces are meant to provide a blueprint for classes to follow, rather than providing specific functionality. Therefore, the statement "All the methods of an interface are by default abstract" is correct.
12.
Which of the following statement is false about for-each loop in Java?
Correct Answer
D. For-each loop can work only with generic collections
Explanation
The statement "for-each loop can work only with generic collections" is false because the for-each loop can work with any type of array or collection, not just generic collections. It can iterate over arrays, lists, sets, and other types of collections.
13.
HashMap is a Collection class. State TRUE or FALSE.
Correct Answer
A. True
Explanation
The statement is true because HashMap is indeed a Collection class in Java. It is a part of the Java Collections Framework and implements the Map interface, which is a sub-interface of the Collection interface. HashMap is used to store key-value pairs and provides efficient retrieval and storage of elements based on their keys.
14.
Which of the following are true regarding RuntimeException? (Choose 2)
Correct Answer(s)
B. Any class that derives the RuntimeException will always be an unchecked exception
C. RuntimeException does not require a throws declaration
Explanation
Any class that derives the RuntimeException will always be an unchecked exception because RuntimeException is a subclass of Exception and all subclasses of RuntimeException are unchecked exceptions. RuntimeException does not require a throws declaration because it is an unchecked exception and does not need to be declared in the method signature or handled explicitly.
15.
Consider the following interface declaration:
interface A{void main(String[] args);}
interface B{public void main(String[] args);}
interface C{public static void main(String[] args);}
interface D{protected void main(String[] args);}
interface E{private void main(String[] args);}
Which of the following option gives the valid interface declaration that will compile successfully?
Correct Answer
D. Interface A,B
Explanation
The correct answer is interface A,B because both interface A and interface B have a valid method declaration that will compile successfully. Interface A has a method main with the default access modifier, and interface B has a method main with the public access modifier.
16.
Which of the following statements is TRUE about StringBuffer class?
Correct Answer
B. StringBuffer is a mutable class
Explanation
The statement "StringBuffer is a mutable class" is true because a mutable class can be modified after its creation. StringBuffer allows for the modification of its content, such as appending or deleting characters, without creating a new object. This is in contrast to the String class, which is immutable and cannot be changed once created.
17.
Which of the following statements are true about finalize method?
Correct Answer
D. Finalize will always run before an object is garbage collected
Explanation
The finalize method in Java is called by the garbage collector before an object is garbage collected. It provides an opportunity for the programmer to perform any necessary cleanup or resource deallocation operations before the object is destroyed. However, it is important to note that the finalize method does not directly free memory allocated to an object. The garbage collector is responsible for reclaiming the memory occupied by unreachable objects.
18.
Which of the following statements are true regarding try-catch-finally? (Choose 2)
Correct Answer(s)
A. A catch block can have another try block nested inside
B. An exception which is not handled by a catch block will be handled by subsequent catch blocks
Explanation
A catch block can have another try block nested inside: This statement is true. It is possible to have a try block nested inside a catch block. This allows for additional error handling within the catch block.
An exception which is not handled by a catch block will be handled by subsequent catch blocks: This statement is true. If an exception is not handled by a catch block, it will be passed on to the next catch block in the sequence. This allows for multiple catch blocks to handle different types of exceptions.
19.
Which of the following is true about packages?
Correct Answer
B. Packages can contain both Classes and Interfaces
Explanation
Packages in Java can contain both classes and interfaces. A package is a way to organize related classes and interfaces together. By placing them in the same package, they can be easily accessed and used within the package without the need for import statements. This allows for better organization and encapsulation of code. Therefore, the statement "Packages can contain both Classes and Interfaces" is true.
20.
Consider the following Statements:
Statement A: Anonymous inner class can extend a class and implement an interface at the same time .
Statement B: Anonymous class can have their own members.
Which of the following option is true regarding the above statements?
Correct Answer
A. Both the statements are true
Explanation
Both statements are true. An anonymous inner class can extend a class and implement an interface simultaneously. This allows for combining the functionalities of both the extended class and implemented interface in the anonymous inner class. Additionally, anonymous classes can have their own members, including fields, methods, and constructors. This allows for customization and specific behavior within the anonymous class.
21.
Which of the following option gives the valid collection implementation class that implements the List interface and also provides the additional methods to get, add and remove elements from the head and tail of the list without specifying an index?
Correct Answer
A. LinkedList
Explanation
LinkedList is the correct answer because it is a valid collection implementation class that implements the List interface. It also provides additional methods such as getFirst(), getLast(), addFirst(), addLast(), removeFirst(), and removeLast() which allow elements to be accessed and modified from both the head and tail of the list without specifying an index.
22.
Consider the following code:
1 public class FinallyCatch {
2 public static void main(String args[]) {
3 try {
4 throw new java.io.IOException();
5 }
6 }
7 }
Which of the following is true regarding the above code?
Correct Answer
C. Demands a finally block at line number 5
Explanation
The correct answer is "Demands a finally block at line number 5". This is because the code in the try block throws an IOException, which is a checked exception. According to Java's exception handling rules, when a checked exception is thrown, it must either be caught using a catch block or declared to be thrown by the method using the throws keyword. In this case, there is no catch block to handle the IOException, so the code demands a finally block at line number 5 to handle any necessary cleanup operations before the exception is propagated further.
23.
Consider the following code :
class BigLength {
public int getLength() {return 4;}
}
public class SmallLength extends BigLength {
public long getLength() {return 5;}
public static void main(String args[]) {
BigLength s =new BigLength();
SmallLength sb=new SmallLength();
System.out.println(s.getLength()+”,”+sb.getLength() };
} }
Which of the following option gives the valid output for the above code?
Correct Answer
B. Compilation error
Explanation
The code will result in a compilation error because the method `getLength()` in the `SmallLength` class has a different return type (`long`) compared to its superclass (`int`). In Java, when overriding a method, the return type must be the same or a subtype of the return type in the superclass. Since `long` is not a subtype of `int`, the code will not compile.
24.
Which of the following options are the methods NOT available in StringBuffer class? (Choose 2)
Correct Answer(s)
A. Append(short s)
B. Append(byte b)
Explanation
The methods append(short s) and append(byte b) are not available in the StringBuffer class. The StringBuffer class provides methods to append various data types such as int, boolean, and long, but it does not have specific methods for appending short or byte data types.
25.
Consider the following code:
public class ProblemsWorld {
public static void main(String args[]) {
try {
xpect();
} catch(IOException e) {
System.out.println(“xpected caught”);
}
}
public static void xpect() throws IOException {
throw new FileNotFoundException();
}
}
Which of the following statement is true regarding the above code?
Correct Answer
D. Compiles and Runs successfully and prints “xpected caught”
Explanation
The code will compile and run successfully because the main method catches the IOException that is thrown by the xpect() method. When the xpect() method is called, it throws a FileNotFoundException, which is a subclass of IOException. Since the catch block in the main method catches IOException, it will also catch the FileNotFoundException. Therefore, the code will print "xpected caught" and continue to run without any errors.
26.
Consider the following code:
interface dumb {} interface Silent{}
class Base implements dumb {}
class Derived extends Base implements Silent {}
public class Test {
public static void main(String args[]) {
Base[] base={new Base()};//Line no 1
Derived dev[]= {new Derived()};//Line no 2
Object obj =dev; //Line no 3
Base = obj;//Line no 4
} }
At the time of compilation the above mentioned code generates some error.
Which of the following option gives the line no where the error is generated?
Correct Answer
B. Line no 4
Explanation
Line no 4 generates an error because we are trying to assign an object of type Object to a variable of type Base, which is not allowed. The variable "obj" is declared as an Object, and it cannot be directly assigned to a variable of type Base without explicit casting.
27.
Arrays can be used as variable arguments parameter type. State True or False.
Correct Answer
A. True
Explanation
Arrays can be used as variable arguments parameter type. This means that a method can accept a variable number of arguments, and one of the arguments can be an array. The array can then be accessed and manipulated within the method. This allows for flexibility in the number of arguments passed to the method, as well as the ability to work with arrays of different sizes. Therefore, the statement "Arrays can be used as variable arguments parameter type" is true.
28.
Which statement is true for the class java.util.ArrayList?
Correct Answer
A. The elements in the collection are ordered.
Explanation
The correct answer is that the elements in the collection are ordered. This means that the elements in an ArrayList are stored in a specific order and can be accessed by their index. Unlike some other collection classes, the order of elements in an ArrayList is maintained and will not change unless explicitly modified by the programmer.
29.
What is the output of the following code?
1 String str = “Welcome”
2 str.concat(“ to Java!”);
3 System.out.println(str);
Correct Answer
C. Prints “Welcome”.
Explanation
The output of the code will be "Welcome". This is because the concat() method in line 2 does not modify the original string "str", but instead returns a new string that is the concatenation of "str" and " to Java!". However, this new string is not assigned to any variable or printed, so the original string "str" remains unchanged.
30.
The following declaration(as a member variable) is legal.
static final transient int maxElements =100;
Correct Answer
A. True
Explanation
The given declaration is legal because it combines the keywords static, final, and transient with the data type int and the variable name maxElements. The keyword static means that the variable is associated with the class itself rather than with any specific instance of the class. The keyword final means that the variable's value cannot be changed once it is assigned. The keyword transient means that the variable is not serialized when the object is serialized. Since all these keywords are valid for member variables, the given declaration is legal.
31.
Choose the correct option:
Correct Answer
B. Integer is a wrapper class
Explanation
The given statement is correct. Integer is indeed a wrapper class in Java. Wrapper classes are used to convert primitive data types into objects. Integer is the wrapper class for the int data type. It provides methods and functionality to manipulate and perform operations on int values.
32.
Which of the following is an implementation of Map interface:
Correct Answer
C. Hashtable
Explanation
Hashtable is an implementation of the Map interface because it allows key-value pairs to be stored and accessed. It provides methods to add, remove, and retrieve elements based on their keys. Hashtable also ensures that keys are unique and does not allow null keys or values. It is a synchronized collection, making it thread-safe for use in multi-threaded environments.
33.
Which of the following returns a primitive data type?(choose 2)
Correct Answer(s)
B. Integer.intValue()
E. Integer.parseInt()
Explanation
The methods Integer.intValue() and Integer.parseInt() both return primitive data types. Integer.intValue() returns the value of the Integer object as an int primitive type, while Integer.parseInt() returns the parsed integer value of a string as an int primitive type.
34.
What is the advantage of runtime polymorphism?
Correct Answer
B. Code flexibility at runtime
Explanation
Runtime polymorphism allows for code flexibility at runtime. This means that the behavior of an object can be changed dynamically during program execution. This flexibility is achieved through method overriding, where a subclass can provide its own implementation of a method defined in its superclass. This allows for different objects to be treated interchangeably, enhancing code flexibility and making it easier to modify and extend the functionality of a program without impacting existing code.
35.
Constructors can be declared as private. State true or false.
Correct Answer
A. True
Explanation
Constructors can be declared as private in order to restrict the creation of objects of a class from outside the class itself. This can be useful in certain scenarios where the class wants to control the creation of its own objects and prevent direct instantiation by other classes or external code. By declaring the constructor as private, only methods within the class can create objects of that class, ensuring that the class has full control over the object creation process.
36.
TreeSet uses interface to sort the data
Correct Answer
C. Comparable
Explanation
The correct answer is Comparable. TreeSet uses the Comparable interface to sort the data. This interface allows objects to define their natural ordering by implementing the compareTo() method. By implementing Comparable, objects can be compared and sorted based on their own defined criteria. TreeSet uses this compareTo() method to arrange the elements in ascending order according to the natural ordering defined by the objects.
37.
Which is right?
Correct Answer
B. Iterator i= HashMap.entrySet().Iterator();
Explanation
The correct answer is "Iterator i= HashMap.entrySet().Iterator();". This is because the entrySet() method returns a Set view of the mappings contained in the HashMap, and calling the iterator() method on this Set returns an iterator over the entries. Therefore, this line of code correctly initializes the iterator to iterate over the entries of the HashMap. The other options are incorrect because they either do not exist (HashMap.Iterator()) or do not return an iterator over the entries of the HashMap (HashMap.TreeSet().Iterator()).
38.
Consider the following statements:
A. Every floating-point literal is implicitly a double, not a float.
B. In the declaration byte b=120; int literal 120 is implicitly converted to byte.
Which of the following option is valid regarding the above statements?
Correct Answer
C. Only A is true
Explanation
The given answer states that only statement A is true. This means that every floating-point literal is implicitly a double, not a float. This is true because in Java, by default, floating-point literals are treated as double unless specified otherwise. Statement B, on the other hand, is false. In the given declaration byte b=120;, the int literal 120 is not implicitly converted to byte. Instead, a type casting is required to explicitly convert the int literal to byte.