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 option at line no 9, "Outer.Inner o = new Outer.Inner();", instantiates an instance of the nested class. It creates an object of the inner class "Inner" using the syntax "Outer.Inner" and assigns it to the variable "o".
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 must have the same name and the same signature, meaning they have the same return type and the same parameters. Additionally, the methods must be in different classes, with the subclass overriding the method from its superclass. Therefore, the correct answers are "Both the methods must not be in the same class" and "Same name same signature".
3.
Which of the following statement is true regarding method overloading?
Correct Answer
D. Methods can be overloaded across inherited classes
Explanation
The statement "Methods can be overloaded across inherited classes" is true regarding method overloading. Method overloading allows multiple methods with the same name but different parameters to exist in the same class or in inherited classes. This means that when a class inherits from another class, it can have its own set of overloaded methods with the same name as the methods in the parent class. This allows for flexibility in the implementation of methods in inherited classes, as they can have different behaviors or handle different types of parameters while still sharing the same method name.
4.
Runtime exception can be handled.
Correct Answer
A. True
Explanation
Runtime exceptions can be handled in a program. Unlike checked exceptions, which are required to be declared or caught, runtime exceptions do not need to be explicitly handled. However, they can still be caught and handled using try-catch blocks. By handling runtime exceptions, the program can gracefully recover from the error and continue executing rather than abruptly terminating.
5.
Unboxing the Numeric Wrapper types to primitive types is done under operations.
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, the question states that unboxing the numeric wrapper types to primitive types is done under certain operations. The operations mentioned are ++ (increment), - (subtraction), and == (equality). These operations require the conversion of the wrapper types to their primitive counterparts in order to perform the desired operation.
6.
Which of the following option is not a valid wrapper type object creation?
Correct Answer
A. New Boolean(“truth”);
Explanation
The given options are all examples of creating wrapper objects for primitive types. However, the statement "new Boolean("truth")" is not a valid way to create a Boolean wrapper object. The Boolean class in Java has two valid ways to create a Boolean object - either by passing a boolean value (true or false) to the constructor or by calling the valueOf() method. Therefore, the statement "new Boolean("truth")" is not a valid wrapper type object creation.
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 output 1. This is because the "==" operator in Java compares the values of the variables. In this case, both "a" and "b" have the same value of 2, so the comparison evaluates to true and the code prints 1.
8.
Which of the following correctly fits for the definition holding instances of other objects?
Correct Answer
B. Generic
Explanation
The term "generic" correctly fits the definition of holding instances of other objects. In programming, a generic type allows for the creation of classes, methods, or interfaces that can work with different types of objects. It provides a way to create reusable code that can handle various data types without the need to specify them explicitly. This allows for greater flexibility and code reusability in situations where the specific type of the object may vary.
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 object of type Error, which is not allowed. The catch block can only catch objects of types that are subclasses of Throwable, and Error is a direct subclass of Throwable. Therefore, the catch block cannot catch an Error object and a compile time error will occur.
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 the elements in a TreeSet must be of the same type and implement the Comparable interface. In this case, the set contains both a string ("Green World") and an integer (1), which are not compatible types.
11.
Select the correct answer
Correct Answer
C. All the methods of an interface are by default abstract
Explanation
All the methods of an interface are by default abstract. This means that the methods declared in an interface do not have an implementation. They only provide a signature or a contract for the classes that implement the interface to follow. Any class that implements an interface must provide an implementation for all the methods declared in the interface. This allows for abstraction and polymorphism in object-oriented programming, as different classes can implement the same interface and provide their own specific implementation for the methods.
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 simplifies the process of iterating over elements in an array or collection by automatically handling the iteration and typecasting.
13.
HashMap is a Collection class.
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. HashMap stores key-value pairs and provides efficient retrieval and storage of elements based on the key. It allows null values and does not maintain the insertion order of elements. Overall, HashMap is a commonly used data structure for storing and accessing data in a key-value format.
14.
Which of the following are true regarding RuntimeException?
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 and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method's throws clause or caught using a try-catch block. Therefore, RuntimeException does not require a throws declaration.
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
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. A mutable class means that its contents can be modified after it is created. StringBuffer class in Java allows for the modification of its contents, 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 modified once created. StringBuffer is often used when there is a need to perform multiple modifications on a string, as it provides better performance and memory efficiency compared to creating multiple String objects.
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 will always run before an object is garbage collected. This method is called by the garbage collector when it determines that there are no more references to the object. It allows the programmer to perform any necessary cleanup or deallocation of resources before the object is destroyed. However, it is important to note that the finalize method does not guarantee that the object will be garbage collected or that it will be collected immediately after the method is called.
18.
Which of the following statements are true regarding try-catch-finally?
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. In Java, a catch block can contain another try block to handle nested exceptions 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 caught and handled by a catch block, it will be passed to the next catch block in the sequence until it is either caught or reaches the end of the try-catch-finally structure.
Finally block cannot have a try block with multiple catch blocks: This statement is false. The finally block can indeed have a try block with multiple catch blocks. This allows for handling different types of exceptions within the same finally block.
An exception which is not handled by a catch block can be handled by writing another try-catch block inside finally block: This statement is false. Exceptions should be handled within the catch block, not within the finally block. Placing a try-catch block inside the finally block is not a recommended practice and can lead to unexpected behavior.
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 into a single unit. It provides a namespace for the classes and interfaces it contains, allowing them to be easily identified and accessed. By grouping classes and interfaces together in a package, it promotes code organization and modularity. This also allows for better code reuse and maintenance. 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 A and B are true. Statement A is true because an anonymous inner class can indeed extend a class and implement an interface simultaneously. This allows the anonymous inner class to inherit the properties and methods of the class it extends, while also implementing the methods of the interface it implements. Statement B is true because an anonymous class can have its own members, such as variables and methods, just like any other 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. Additionally, LinkedList provides additional methods such as getFirst(), getLast(), addFirst(), addLast(), removeFirst(), and removeLast() which allow getting, adding, and removing elements from the head and tail of the list without specifying an index. ArrayList also implements the List interface, but it does not provide these additional methods. List and Collection are interfaces, not implementation classes.
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 given code throws an IOException at line number 4. Since there is no catch block to handle this exception, the code demands a finally block at line number 5 to ensure that any necessary cleanup or resource releasing operations are performed before the program terminates.
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 subclass SmallLength has a different return type (long) than the method getLength() in the superclass BigLength (int). In Java, method overriding requires the return type of the subclass method to be covariant with the return type of the superclass method. Since int and long are not covariant, the code will not compile.
24.
Which of the following options are the methods NOT available in StringBuffer class?
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 various append() methods for appending different types of data 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:
class Testone {
public class Main {
public static void main(String[] args) {
int i=1;
int n=++i%5;
System.out.println(“value of n is:” +n);
n=i--%4;
System.out.println(“value of n is:” +n);
n=i++%2;
System.out.println(“value of n is:” +n);
}
}
Which of the following option gives the output for the above code?
Correct Answer
D. 2,2,1
Explanation
The code first increments the value of i by 1 and then calculates the remainder when i is divided by 5. Since i is 2 after the increment, the remainder is 2. Therefore, the first output is "value of n is: 2".
Next, the code decrements the value of i by 1 and calculates the remainder when i is divided by 4. Since i is 1 after the decrement, the remainder is 1. Therefore, the second output is "value of n is: 1".
Finally, the code increments the value of i by 1 and calculates the remainder when i is divided by 2. Since i is 2 after the increment, the remainder is 0. Therefore, the third output is "value of n is: 0".
Hence, the correct answer is 2,2,1.
26.
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 compiles and runs successfully because the catch block catches the IOException thrown by the xpect() method. Since FileNotFoundException is a subclass of IOException, it is also caught by the catch block. Therefore, the code prints "xpected caught" without any errors.
27.
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 the error because we are trying to assign an Object type variable (obj) to a Base type variable, which is not allowed without casting. The obj variable is declared as Object type, so it can refer to any object. But when we try to assign it to a Base type variable, it requires an explicit type casting.
28.
Arrays can be used as variable arguments parameter type.
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 of the same type, and those arguments can be passed as an array. This is useful when the number of arguments is not known in advance, as it allows for flexibility in the number of arguments that can be passed to the method. By using an array as the variable arguments parameter type, the method can handle any number of arguments effectively.
29.
Which statement is true for the class java.util.ArrayList?
Correct Answer
A. The elements in the collection are ordered.
Explanation
The statement that is true for the class java.util.ArrayList is that the elements in the collection are ordered. This means that the elements are stored in a specific sequence and can be accessed by their index position. The order in which elements are added to the ArrayList is maintained, allowing for easy retrieval and iteration over the elements in the collection.
30.
Which is true about an anonymous inner class?
Correct Answer
C. It can extend exactly one class or implement exactly one interface.
Explanation
An anonymous inner class is a class that is defined and instantiated at the same time, without giving it a name. It can either extend exactly one class or implement exactly one interface. This allows for flexibility in creating a class that has specific behaviors or characteristics. The option that states "It can extend exactly one class or implement exactly one interface" accurately describes the capabilities of an anonymous inner class.
31.
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 code declares a string variable "str" and assigns it the value "Welcome". The "concat" method is called on the string object "str" with the argument " to Java!". However, the "concat" method does not modify the original string, but instead returns a new string that is the concatenation of the original string and the argument. Since this returned string is not stored or printed, the output will still be the original value of "str", which is "Welcome".
32.
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 three modifiers: static, final, and transient. The static modifier means that the variable belongs to the class itself, rather than to any particular instance of the class. The final modifier means that the variable cannot be reassigned once it has been initialized. The transient modifier means that the variable will not be serialized when the object is serialized. Therefore, the declaration of "static final transient int maxElements = 100;" is legal.
33.
Choose the correct option:
Correct Answer
B. Integer is a wrapper class
Explanation
Integer is a wrapper class because it is used to wrap the primitive data type int into an object. Wrapper classes in Java provide a way to use primitive data types as objects. Integer class provides various methods to perform operations on int values, such as converting int to String, parsing String to int, etc.
34.
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 stores key-value pairs and allows efficient retrieval of values based on their corresponding keys. It provides methods to add, remove, and retrieve elements using key operations. Hashtable also ensures that each key is unique and does not allow null keys or values. Therefore, Hashtable meets the requirements of the Map interface and can be used to store and manipulate data in a key-value format.
35.
Which of the following returns a primitive data type?
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 as an int primitive type.
36.
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 determined at runtime based on its actual type, rather than its declared type. This allows for more dynamic and flexible code, as different objects can exhibit different behaviors even when accessed through a common interface or superclass. This flexibility enables easier modification and extension of code, as new behaviors can be added without modifying existing code. It also promotes code reuse, as different objects can share common behaviors through inheritance.
37.
Constructors can be declared as private.
Correct Answer
A. True
Explanation
Constructors can be declared as private in order to restrict the creation of objects of a class to only within the class itself. This means that objects of the class cannot be created from outside the class, but can still be created within the class itself or by other methods or functions within the class. This can be useful in certain scenarios where control over object creation is necessary, such as implementing singleton patterns or factory methods.
38.
TreeSet uses interface to sort the data.
Correct Answer
C. Comparable
Explanation
The correct answer is Comparable. TreeSet is a class that implements the Set interface in Java. It uses the Comparable interface to sort the elements in a natural order. The Comparable interface provides a compareTo() method that allows objects to be compared and sorted based on their natural ordering. By implementing the Comparable interface, objects can define their own custom sorting logic. In the case of TreeSet, it uses the compareTo() method to determine the order of elements in the set.
39.
Which is right?
Correct Answer
B. Iterator i= HashMap.entrySet().Iterator();
Explanation
The correct answer is "Iterator i= HashMap.entrySet().Iterator();". This is because HashMap does not have a method called "Iterator()", so the first option is incorrect. The second option "Iterator i= HashMap.entrySet().Iterator();" is correct because it returns an iterator over the entries in the HashMap. The third option "Iterator i= HashMap.TreeSet().Iterator();" is incorrect because HashMap does not have a method called "TreeSet()".
40.
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
Statement A is true because in Java, every floating-point literal is implicitly considered as a double by default. If we want to declare a float literal, we need to append an 'f' or 'F' at the end of the number.
Statement B is false because in the given declaration, the int literal 120 is not implicitly converted to byte. Instead, it is explicitly assigned to the byte variable 'b'. If the value exceeds the range of the byte data type, a compilation error will occur.