1.
Is it possible to have abstract static method for a class?
Correct Answer
B. No
Explanation
It is not possible to have an abstract static method for a class because an abstract method is meant to be overridden by the subclasses, but a static method belongs to the class itself and cannot be overridden. Therefore, it is not meaningful to declare a static method as abstract.
2.
The return type of constructor must always be void. True or False?
Correct Answer
B. False
Explanation
The return type of a constructor is not specified, not even void. In fact, constructors do not have a return type at all. They are responsible for initializing the object and do not return any value. Therefore, the correct answer is False.
3.
______ command is used to invoke Java compiler
Correct Answer
A. Javac
Explanation
The correct answer is "javac". The javac command is used to invoke the Java compiler. It is used to compile Java source code files (.java) into bytecode files (.class) that can be executed by the Java Virtual Machine (JVM).
4.
Which of the following methods are from Thread class:
Correct Answer(s)
A. Sleep
C. Yield
D. Join
Explanation
The methods sleep, yield, and join are from the Thread class. The sleep method is used to pause the execution of a thread for a specified amount of time. The yield method is used to voluntarily give up the current thread's turn to execute and allow other threads to run. The join method is used to wait for a thread to complete its execution before moving on to the next line of code.
5.
CoreJava java = new CoreJava(){
public void learn(){
System.out.println("YavaJavaYavaJava");
}
};Above code is an example of:
Correct Answer
A. Anonymous inner class
Explanation
The given code is an example of an anonymous inner class. This is because it creates a new class that extends the CoreJava class and overrides its learn() method. The code also creates an instance of this anonymous inner class and calls the learn() method, which prints "YavaJavaYavaJava" to the console.
6.
The generic Type information does not exist at runtime. All generic code is strictly for compiler. This process of removing type information out of the class bytcode is called "Type Erasure". True or False?
Correct Answer
A. True
Explanation
The statement is true. Generic type information is not available at runtime and is only used by the compiler. The process of removing type information from the class bytecode is called "Type Erasure".
7.
Which of the following are examples of sorted collection?
Correct Answer(s)
B. TreeSet
C. TreeMap
Explanation
TreeSet and TreeMap are examples of sorted collections because they both implement the SortedSet and SortedMap interfaces respectively. These interfaces provide methods to maintain the elements in a sorted order based on their natural ordering or a custom comparator. TreeSet stores elements in a sorted order without any duplicates, while TreeMap stores key-value pairs in a sorted order based on the keys. ArrayList and TreeList, on the other hand, are not sorted collections as they do not maintain any specific order for their elements.
8.
ArrayList
Correct Answer(s)
A. Implements RandomAccess (marker) interface
B. Is an ordered but unsorted collection
C. Is faster than vector
Explanation
The correct answer is that ArrayList implements the RandomAccess (marker) interface, is an ordered but unsorted collection, and is faster than vector. The RandomAccess interface is a marker interface that indicates that the implementing class supports fast random access to its elements. ArrayList maintains the order of elements as they are added but does not sort them automatically. It is faster than vector because it is not synchronized, which means it does not incur the overhead of thread-safety. Additionally, ArrayList allows duplicate elements.
9.
Which of the following is/are true statements regarding equals method?
Correct Answer(s)
A. If X equals Y and Y equals Z then X equals Z
B. X.equals(null) will return false
D. Null.equals(x) will give NullPointerException
Explanation
The first statement is true because if X equals Y and Y equals Z, then transitivity dictates that X must also equal Z.
The second statement is true because the equals method typically returns false when comparing an object to null.
The third statement is also true because calling the equals method on a null object will result in a NullPointerException.
10.
If you want to save the state of your object, you can implement ________ interface
Correct Answer
C. Serializable
Explanation
If you want to save the state of your object, you can implement the Serializable interface. Serializable is a marker interface in Java that allows objects to be converted into a byte stream, which can then be saved to a file or sent over a network. By implementing the Serializable interface, the object's state can be persisted and later restored, making it suitable for tasks like object serialization and deserialization.
11.
What would be the output:String website = "www.YavaYavaJavaJava.com";
System.out.println(s.substring(4,20));
Correct Answer
A. YavaYavaJavaJava
Explanation
The given code snippet creates a string variable "website" with the value "www.YavaYavaJavaJava.com". The next line of code uses the substring method to extract a portion of the string, starting from index 4 and ending at index 20. This will result in the output "YavaYavaJavaJava", which is the substring of the original string that falls within the specified range.
12.
How many String objects would be created in following code:String s1 = "YavaYava";
String s2 = "JavaJava";
String s3 = ".com";
s1 = "www." + s1 + s2 + s3;
String s4 = new String("www.YavaYavaJavaJava.com");
Correct Answer
C. 6
Explanation
In the given code, a total of 6 String objects would be created.
1. The first String object is created when the string "YavaYava" is assigned to the variable s1.
2. The second String object is created when the string "JavaJava" is assigned to the variable s2.
3. The third String object is created when the string ".com" is assigned to the variable s3.
4. The fourth String object is created when the concatenation of "www.", s1, s2, and s3 is assigned to the variable s1.
5. The fifth String object is created when the string "www.YavaYavaJavaJava.com" is passed as an argument to the String constructor, creating a new String object assigned to the variable s4.
6. The sixth String object is created when the concatenation of "www.", s1, s2, and s3 is assigned to the variable s1 again.
13.
Which of the following is/are true statement(s)?
Correct Answer(s)
A. StringBuffer is slower than StringBuilder
B. StringBuffer is synchronized while StringBuilder is not
C. Strings are immutable objects
Explanation
The first statement is true because StringBuffer is slower than StringBuilder due to its synchronization feature, which adds overhead to its operations. The second statement is also true as StringBuffer is synchronized, meaning that it is thread-safe and can be accessed by multiple threads simultaneously without causing any data inconsistency issues. The third statement is true as well because strings in Java are immutable, meaning that their values cannot be changed once they are created. The fourth statement is false because the choice between StringBuffer and StringBuilder depends on the specific requirements of the program.
14.
assert keyword was introduced in Java version ____
Correct Answer
A. 1.4
Explanation
The assert keyword was introduced in Java version 1.4.
15.
Which of the following are checked exceptions?
Correct Answer(s)
C. IllegalStateException
D. FileNotFoundException
Explanation
NullPointerException and ArrayIndexOutOfBoundsException are not checked exceptions because they are subclasses of RuntimeException, which is an unchecked exception. However, IllegalStateException and FileNotFoundException are checked exceptions because they are not subclasses of RuntimeException and must be declared in the method signature or handled using try-catch blocks.
16.
Is below code valid?public void getValue(){
try{
System.out.println("Hi");
}catch(OutOfMemoryError e){
System.out.println("Error occured");
}finally{
System.out.println("Finally always executes");
}
}
Correct Answer
A. Yes
Explanation
The given code is valid because it follows the correct syntax for a method declaration. The method "getValue()" does not have any parameters and has a return type of "void". Inside the method, there is a try-catch-finally block. The try block contains a print statement that will always execute and print "Hi". The catch block is catching an "OutOfMemoryError" exception, but since it is not being thrown in the try block, it will not be executed. Finally, the finally block will always execute and print "Finally always executes". Therefore, the code is valid.
17.
Which of the below for loops are valid?
Correct Answer(s)
A. For ( ; ; ;){}
C. For (int i = 0; i < 10; i--){ }
Explanation
The first valid for loop is "for ( ; ; ;){}". This is a valid infinite loop as it does not have any initialization, condition, or iteration expression. It will continue to execute indefinitely unless there is a break or return statement inside the loop.
The second valid for loop is "for (int i = 0; i < 10; i--){ }". This loop initializes the variable i to 0, checks if i is less than 10, and then decrements i by 1 in each iteration. This loop will run 10 times, with i taking the values 0, -1, -2, -3, and so on until -9.
18.
What would be the output of below code:String string = "Hi";
switch(string.length()){
case 1:
System.out.print("One ");
case 2:
System.out.print("Two ");
case 3:
System.out.print("Three ");
default:
System.out.print("Default ");
}
Correct Answer
D. Two Three Default
Explanation
The output of the code will be "Two Three Default". This is because the length of the string is 2, so the case 2 block will be executed and "Two " will be printed. Since there are no break statements after each case, the code will continue to execute the following cases. Therefore, the case 3 block will also be executed and "Three " will be printed. Finally, the default block will be executed and "Default " will be printed.
19.
_____________ operator can be used to check if object is of particular type
Correct Answer
A. Instanceof
Explanation
The instanceof operator can be used to check if an object is of a particular type. It returns true if the object is an instance of the specified type, and false otherwise.
20.
Convert below code using ternary operators:String x = "";
if(i > 10){
x = "Hello";
}else {
x = "World"
}
Correct Answer
A. String x = (i > 10) ? "Hello" : "World";
Explanation
The correct answer is "String x = (i > 10) ? "Hello" : "World";". This is the correct conversion of the given code using ternary operators. It checks if the variable "i" is greater than 10, and if it is, assigns the value "Hello" to the string variable "x". If it is not, it assigns the value "World" to "x".
21.
___________ method is a mechanism to run some code just before object is deleted by garbage collector.
Correct Answer
A. Finalize
Explanation
The finalize method is a mechanism in Java that allows code to be executed just before an object is deleted by the garbage collector. This method is called by the garbage collector when it determines that there are no more references to the object. It can be used to perform any necessary cleanup or resource release operations before the object is destroyed.
22.
Which of the following is/are correct statement(s)
Correct Answer(s)
A. Instance variables live on heap
B. Local variables live on stack
D. Objects live on heap
Explanation
Instance variables live on the heap because they are associated with an instance of a class and are created when an object is instantiated. Local variables, on the other hand, live on the stack because they are created and destroyed within the scope of a method or block. Objects also live on the heap because they are dynamically allocated and deallocated using the "new" keyword.
23.
Methods can be overloaded or overriden while constructors can
Correct Answer(s)
A. Be overloaded
D. Not be overriden
Explanation
Constructors cannot be overridden because they are not inherited like methods. Each class has its own constructor, and when a subclass is created, the superclass constructor is not automatically called. However, constructors can be overloaded, which means that a class can have multiple constructors with different parameters. Overloading allows for the creation of objects with different initializations based on the parameters passed to the constructor.
24.
Good Object Oriented design should
Correct Answer
A. Have loose coupling but high cohesion
Explanation
Good Object Oriented design should have loose coupling but high cohesion. Loose coupling means that the components of the system are not tightly dependent on each other, allowing for easier maintenance, testing, and flexibility. High cohesion means that the components within a module are closely related and focused on a single task or responsibility. This promotes better code organization and readability. Therefore, a design with loose coupling but high cohesion is considered a good practice in Object Oriented design.
25.
____________ Allows code defined in one class to be reused in other classes
Correct Answer
A. Inheritance
Explanation
Inheritance allows code defined in one class to be reused in other classes. It is a fundamental concept in object-oriented programming where a class can inherit properties and methods from another class, known as the parent class or superclass. This allows for code reuse, as the child class can inherit the behavior of the parent class and also add its own unique behavior. Inheritance promotes code organization, modularity, and extensibility.
26.
Constructors can be private. True or False?
Correct Answer
A. True
Explanation
Constructors can be made private in order to restrict the creation of objects of a class to only within the class itself. This can be useful in certain scenarios where the class wants to control the object creation process and prevent it from being instantiated by external code. By making the constructor private, it can only be accessed and used by other methods within the class, ensuring that the object creation is done according to the class's requirements and restrictions.
27.
Which of the following is/are illegal definition(s) of a method?
Correct Answer(s)
B. Public abstract getValue(){}
D. Public abstract final getValue();
Explanation
Both "public abstract getValue(){}" and "public abstract final getValue();" are illegal definitions of a method.
In the first option, "public abstract getValue(){}", the method is declared as abstract, which means it does not have a body. However, it is also defined with curly braces {}, indicating that it should have an implementation. This is contradictory and therefore illegal.
In the second option, "public abstract final getValue();", the method is declared as both abstract and final. These two modifiers are mutually exclusive. Abstract methods are meant to be overridden in subclasses, while final methods cannot be overridden. Therefore, combining them in the same method definition is not allowed.
28.
What would be the output of following code:Class Animal {
public void eat(){
System.out.println("Animal is eating");
}
}
Class Horse extends Animal(){
public void eat(){
System.out.println("Horse is eating")
}
}
public Class Application{
public static void main(String [] args){
Animal animal = new Horse();
animal.eat();
}
}
Correct Answer
B. Horse is eating
Explanation
The output of the code would be "Horse is eating". This is because the variable "animal" is declared as type "Animal" but is assigned a new instance of the "Horse" class. Since the "eat" method is overridden in the "Horse" class, the version of the method in the "Horse" class is called when the "eat" method is invoked on the "animal" object.
29.
If Horse extends Animal then which of the following is correct?
Correct Answer(s)
A. Horse horse = new Horse();
B. Animal horse = new Animal();
C. Animal horse = new Horse();
Explanation
If Horse extends Animal, then it is correct to say that Horse horse = new Horse(), Animal horse = new Animal(), and Animal horse = new Horse(). This is because when a class extends another class, an object of the child class can be assigned to a variable of the parent class. In this case, since Horse extends Animal, a Horse object can be assigned to a variable of type Horse or Animal, and an Animal object can be assigned to a variable of type Animal. However, it is not correct to say Horse horse = new Animal(), as an object of the parent class cannot be assigned to a variable of the child class.
30.
A extends B is correct only if..
Correct Answer(s)
A. A is a class and B is a class
C. A is an interface and B is an interface
Explanation
The correct answer is that A extends B is correct only if A is a class and B is a class, or if A is an interface and B is an interface. This is because in object-oriented programming, the "extends" keyword is used to establish an inheritance relationship between classes, where a subclass inherits the properties and behaviors of its superclass. Similarly, interfaces can also extend other interfaces, allowing them to inherit method signatures. Therefore, for the statement "A extends B" to be correct, both A and B must be either classes or interfaces.
31.
If you are marking an instance variable as _________, you're telling the JVM to ignore this variable when you attempt to serialize the object containing it.
Correct Answer
B. Transient
Explanation
By marking an instance variable as "transient", you are instructing the JVM to exclude this variable from the serialization process. Serialization is the process of converting an object into a byte stream to store it in memory or transfer it over a network. When an object is serialized, all of its instance variables are also serialized by default. However, by declaring a variable as transient, you are indicating that it should not be serialized along with the object. This is useful when there are certain variables that do not need to be persisted or when their values can be derived or obtained again upon deserialization.
32.
___________ modifier indicates that a method is implemented in platform-dependent code
Correct Answer
B. Native
Explanation
The correct answer is "native". In Java, the "native" modifier is used to indicate that a method is implemented in platform-dependent code, typically written in another programming language such as C or C++. This allows the method to interact directly with the underlying system or hardware, providing low-level functionality that may not be available in pure Java code.
33.
______________ keyword indicates that a method can be accessed by only one thread at a time.
Correct Answer
A. Synchronized
Explanation
The keyword "synchronized" indicates that a method can be accessed by only one thread at a time. This means that multiple threads cannot simultaneously execute the synchronized method, ensuring that only one thread can access and modify the shared data at any given time. This helps prevent race conditions and ensures thread safety in concurrent programming.
34.
The first concrete subclass of an abstract class must implement all abstract methods of the superclass. True or False?
Correct Answer
A. True
Explanation
In object-oriented programming, an abstract class is a class that cannot be instantiated and is meant to be subclassed. Abstract methods are declared in the abstract class but do not have an implementation. When a concrete subclass extends an abstract class, it must provide implementations for all the abstract methods defined in the superclass. This is because the concrete subclass is responsible for providing the missing functionality that the abstract methods represent. Therefore, the statement is true.
35.
If there is a public class in a source file, the name of the file must match the name of the public class. True or False?
Correct Answer
A. True
Explanation
In Java, when there is a public class in a source file, the name of the file must match the name of the public class. This is because the Java compiler expects the source file to have the same name as the public class in order to properly compile and run the code. If the names do not match, it will result in a compilation error. Therefore, the given answer "True" is correct.