1.
Which is the valid declarations within an interface definition?
Correct Answer
A. Public double methoda();
Explanation
Option A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract.
Option B is wrong. The final modifier means that this method cannot be constructed in a subclass. A final method cannot be abstract.
Option C is wrong. static is concerned with the class and not an instance.
2.
Which is right way to creating an array of integer in java?
Correct Answer
C. Int[] javaArray = new int[10];
Explanation
The correct way to create an array of integers in Java is by using the syntax "int[] javaArray = new int[10];". This declares an integer array named "javaArray" with a length of 10. The "int[]" specifies that it is an array of integers, and "new int[10]" creates a new array object with 10 elements.
3.
Which keyword is used to inherit class?
Correct Answer
B. Extends
Explanation
The keyword "extends" is used to inherit a class in object-oriented programming. Inheritance allows a class to inherit the properties and methods of another class, known as the superclass or parent class. By using the "extends" keyword, a subclass or child class can inherit all the non-private members of the superclass. This allows for code reuse, as the subclass can add additional functionality or override existing methods from the superclass.
4.
Which of the java classes that cannot be subclassed?
Correct Answer
C. Final class
Explanation
A final class in Java cannot be subclassed because it is declared with the "final" keyword, which means it cannot be extended by any other class. This is useful when you want to prevent any further modification or extension of a class, ensuring that its implementation remains unchanged. Therefore, a final class is the correct answer as it cannot be subclassed.
5.
Can we declare abstract static method?
Correct Answer
B. No
Explanation
No, we cannot declare an abstract static method. Abstract methods are meant to be overridden in the subclass, but static methods belong to the class itself and cannot be overridden. The combination of abstract and static modifiers is not allowed in Java. Abstract methods must be declared in an abstract class or interface, and they require the subclass to provide an implementation.
6.
Which type of inheritance is not supported by java?
Correct Answer
B. Multiple
Explanation
Java does not support multiple inheritance, which means a class cannot inherit from multiple classes at the same time. This is because multiple inheritance can lead to complex situations such as diamond problem, where a class inherits from two classes that have a common superclass. To avoid such complications, Java uses interfaces to achieve multiple inheritance-like behavior, where a class can implement multiple interfaces but can only extend one class.
7.
Given interface declaration
interface Base
{
boolean m1 ();
byte m2(short s);
}
which code fragment will compile?
Correct Answer
D. Abstract class Class2 implements Base
{
public boolean m1()
{ return (7 > 4); }
}
Explanation
The correct answer is "abstract class Class2 implements Base { public boolean m1() { return (7 > 4); } }". This code fragment will compile because it is implementing the interface Base and providing an implementation for the m1() method. The implementation returns a boolean value based on the condition (7 > 4).
8.
Java final methods cannot be overridden but overloaded?
Correct Answer
A. True
Explanation
In Java, the final keyword is used to indicate that a method cannot be overridden in a subclass. This means that once a method is declared as final in a superclass, it cannot be modified or overridden in any of its subclasses. However, final methods can still be overloaded in the same class or in its subclasses. Overloading refers to having multiple methods with the same name but different parameters in the same class or its subclasses. Therefore, the statement "Java final methods cannot be overridden but overloaded" is true.
9.
An abstract class should have methods all declared abstract?
Correct Answer
B. No
Explanation
No, an abstract class can have both abstract methods and non-abstract methods. Abstract methods are declared without any implementation and must be implemented by the concrete subclasses. Non-abstract methods in an abstract class can have their own implementation and can be used by the subclasses. This allows for code reuse and provides a common base for the subclasses to inherit from.
10.
The finalize() method present in Object class is called just prior to
Correct Answer
D. Before garbage collection
Explanation
The finalize() method in the Object class is called before garbage collection. This means that when an object is no longer referenced and is eligible for garbage collection, the finalize() method is invoked just before it is actually collected by the garbage collector. This method can be used to perform any necessary cleanup or resource releasing tasks before the object is destroyed.
11.
Given the below class definitions
class Base
{
void display ()
{
System.out.println("Base"); }
}
class Derived extends Base
{
void display ()
{ System.out.println("Derived"); }
}
and objects
Base b = new Base();
Derived d = new Derived();
Base bd = new Derived();
then the print statements
System.out.print(b.display() + " ");
System.out.print(d.display() + " ");
System.out.print(bd.display() + " ");
System.out.println();
will display:
Correct Answer
B. Base Derived Derived
Explanation
The print statements will display "Base Derived Derived" because when the display() method is called on the object b, it prints "Base". When the display() method is called on the object d, it prints "Derived". When the display() method is called on the object bd, it is still a Derived object, so it prints "Derived".
12.
Which of the following are primitive types?
Correct Answer
A. Byte
Explanation
The primitive types in programming refer to the basic data types that are built-in and not derived from any other type. In this case, the correct answer is "byte" because it is a primitive type in many programming languages, including Java. String, Integer, and Float are not primitive types, as they are classes or objects that provide additional functionality beyond basic data storage.
13.
Which of the following statements accurately describe the use of access modifiers(private,public,protected) within a class definition?
Correct Answer(s)
A. They can be applied to both data & methods
B. They must precede a class's data variables or methods
D. They can appear in any order
Explanation
Access modifiers (private, public, protected) can be applied to both data and methods within a class definition. They must precede a class's data variables or methods and can appear in any order. However, there is no requirement that they must be applied to data variables first and then to methods.
14.
Which of the following statements correctly describes the relation between an object and the instance variable it stores?
Correct Answer(s)
A. Each new object has its own distinctive set of instance variables
B. Each object has a copy of the instance variables of its class
C. The instance variable of each object are seperate from the variables of other objects
Explanation
Each new object has its own distinctive set of instance variables because each object is created from a class blueprint and has its own unique state. Each object has a copy of the instance variables of its class because instance variables are defined within a class and each object of that class will have its own copy of those variables. The instance variables of each object are separate from the variables of other objects because they are stored within the memory space allocated to each individual object and can be accessed and modified independently.
15.
The finally block is executed when an exception is thrown, even if no catch matches it.
Correct Answer
A. Yes
Explanation
The finally block in Java is used to execute a set of statements regardless of whether an exception is thrown or not. It ensures that certain code is always executed, even if an exception occurs. In this case, the correct answer is "Yes" because the finally block will be executed even if no catch block matches the thrown exception. This is important for tasks such as closing resources or releasing locks, which need to be done regardless of whether an exception occurs.
16.
Class Test
{
public static void main(String[] args){
Object obj;
obj.toString();
}
}
Correct Answer
A. NullPointerException
Explanation
In the given code, a NullPointerException will occur. This is because the variable "obj" is declared but not initialized with any object. When the method "toString()" is called on an uninitialized object, it will throw a NullPointerException.
17.
Which of the following are the new type in Java
Correct Answer
C. Both Class and Interface
Explanation
In Java, both classes and interfaces are considered new types. A class is a blueprint for creating objects, while an interface is a collection of abstract methods that can be implemented by classes. Both classes and interfaces are used to define the structure and behavior of objects in Java. Therefore, the correct answer is "Both Class and Interface."
18.
Is Floating point operations is supported in MCHAI JVM(CISCO)
Correct Answer
B. No
Explanation
The explanation for the answer "No" is that the MCHAI JVM (Cisco) does not support floating point operations. This means that any calculations or operations involving floating point numbers cannot be performed in this JVM.
19.
What is the size of int in java
Correct Answer
B. 4 bytes
Explanation
The size of an int in Java is 4 bytes. In Java, the int data type is a 32-bit signed integer, which means it can hold values ranging from -2,147,483,648 to 2,147,483,647. The size of an int is fixed and does not depend on the platform or operating system being used.
20.
Is multiple Inheritance is supported in case of Interfaces?
Correct Answer
A. Yes
Explanation
Multiple inheritance is supported in case of interfaces. In object-oriented programming, multiple inheritance refers to a class inheriting properties and behaviors from multiple parent classes. However, some programming languages do not support multiple inheritance for classes, as it can lead to complexity and ambiguity. Interfaces, on the other hand, provide a way to achieve multiple inheritance in a controlled manner. A class can implement multiple interfaces, allowing it to inherit and define multiple sets of properties and behaviors. Therefore, the correct answer is Yes.
21.
Which of the following is true about class defination in a file
Correct Answer
D. All of above
Explanation
In Java, it is possible to define multiple classes in a single file. However, there can be only one public class in that file, and the name of the public class must match the name of the file. Therefore, all of the given statements are true about class definition in a file.
22.
Which of the following is wrapper classes in java
Correct Answer(s)
A. Integer
B. Float
D. Byte
Explanation
Wrapper classes in Java are classes that encapsulate primitive data types and provide useful methods and functionalities. They allow primitive data types to be treated as objects. In this case, Integer, Float, and Byte are all examples of wrapper classes in Java. They provide additional methods and functionalities that are not available with primitive data types, such as parsing, converting, and performing mathematical operations. String, on the other hand, is not a wrapper class as it is a class that represents a sequence of characters and is used to manipulate and store textual data.
23.
What is the output of following code
int x[]={1,2,3,4};
Sytem.out.println(" Index at 4 is "+x[4]);
Correct Answer
A. IndexOutOfBoundException
Explanation
The code is trying to access the element at index 4 of the array "x", but the array only has indices 0, 1, 2, and 3. Therefore, it will throw an IndexOutOfBoundsException.
24.
What is the base class of all Java classes?
Correct Answer
A. Object
Explanation
The base class of all Java classes is the Object class. This class is at the top of the Java class hierarchy and is implicitly inherited by all other classes in Java. It provides a set of methods that are common to all objects, such as toString(), equals(), and hashCode(). By inheriting from the Object class, all Java classes have access to these methods and can override them as needed.
25.
Given the below code. What is the result?
class Test
{
public static void main(String args[]) {
String str = "null";
if (str == null) {
System.out.print("1");
} else if (str.length() == 0) {
System.out.print("2");
} else {
System.out.print("3");
}
}
}
Correct Answer
D. "3" is printed.
Explanation
The code initializes a String variable "str" with the value "null". The if statement checks if "str" is equal to null, which is true. Therefore, the code inside the if block is executed, and "1" is not printed. The else if statement checks if the length of "str" is equal to 0, which is false. Therefore, the code inside the else if block is not executed, and "2" is not printed. Since none of the previous conditions are met, the code inside the else block is executed, and "3" is printed. Therefore, the correct answer is "3" is printed.
26.
In the below code which can directly access and change the value of the variable id?
package com.cisco.mycompany;
public class Company {
private int id = 100;
}
Correct Answer
A. Only the Company class
Explanation
The variable "id" in the code is declared as private, which means it can only be accessed and modified within the same class. Therefore, only the Company class itself can directly access and change the value of the variable "id".
27.
Given the code. What is the result after the class TryMe execution?
class A {
public void doA() {
B b = new B();
b.dobB();
System.out.print("doA");
}
}
class B {
public void dobB() {
C c = new C();
c.doC();
System.out.print("doB");
}
}
class C {
public void doC() {
if (true)
throw new NullPointerException();
System.out.print("doC");
}
}
public class TryMe {
public static void main(String args[]) {
try {
A a = new A();
a.doA();
} catch (Exception ex) {
System.out.print("error");
}
}
}
Correct Answer
D. "error" is printed
Explanation
The code throws a NullPointerException in the doC() method of class C because the if statement condition is always true. This exception is caught in the catch block in the main method of class TryMe. Therefore, the program prints "error" as the result.
28.
Given the code. What is the result if NullPointerException occurs at line 2?
1. try {
2. //some code goes here
3. }
4. catch (NullPointerException ne) {
5. System.out.print("1 ");
6. }
7. catch (RuntimeException re) {
8. System.out.print("2 ");
9. }
10. finally {
11. System.out.print("3");
12. }
Correct Answer
C. 1 3
Explanation
If a NullPointerException occurs at line 2, the catch block at line 4 will be executed. This catch block will print "1 " to the console. After that, the finally block at line 10 will be executed and it will print "3" to the console. So the result will be "1 3".
29.
Given the code. What is the result?
class Vehicle {
public void printSound() {
System.out.print("vehicle");
}
}
class Car extends Vehicle {
public void printSound() {
System.out.print("car");
}
}
class Bike extends Vehicle {
public void printSound() {
System.out.print("bike");
}
}
public class Test {
public static void main(String[] args) {
Vehicle v = new Car();
Bike b = (Bike) v;
v.printSound();
b.printSound();
}
}
Correct Answer
B. An ClassCastException is thrown at runtime.
Explanation
In the given code, a Vehicle object v is created and assigned a new Car object. Then, a Bike object b is created by casting v to a Bike object. However, since v is actually a Car object, it cannot be cast to a Bike object. This results in a ClassCastException being thrown at runtime.
30.
What is the output of following code
int a=10;
int b=10;
System.out.println("The sum of a and b is "+a+b);
Correct Answer
B. The sum of a and b is 1010
Explanation
The output of the code is "The sum of a and b is 1010" because the code uses the concatenation operator (+) to combine the string "The sum of a and b is " with the values of variables a and b, which are both integers. When concatenating a string with an integer, the integer is converted to a string. In this case, the code first concatenates "The sum of a and b is " with the value of a (10), resulting in "The sum of a and b is 10". Then it concatenates this string with the value of b (10), resulting in "The sum of a and b is 1010".
31.
Which of the follwing exe in java bin folder is java virtual machine that will ne used to launch java program
Correct Answer
B. Java
Explanation
The correct answer is "java" because it is the executable file in the Java bin folder that is responsible for launching Java programs. The "java" command is used to start the Java Virtual Machine (JVM), which then executes the bytecode of the Java program. The other options listed are not responsible for launching Java programs. "javac" is the Java compiler, "javah" is used for generating C header files, and "javadoc" is used for generating documentation from Java source code.
32.
Which of the following class is not part of java Collection Framework
Correct Answer
D. String
Explanation
The class "String" is not part of the Java Collection Framework because it does not implement the Collection interface or any of its subinterfaces (List, Set, Queue). The Java Collection Framework is a set of classes and interfaces that provide implementations of common data structures like lists, sets, and maps. While String is a commonly used class in Java for representing textual data, it is not a collection class and does not provide methods for adding, removing, or manipulating elements like the other options (Vector, ArrayList, HashMap) do.
33.
Which is the Collection Framework class to store key and value?
Correct Answer
A. HashMap
Explanation
HashMap is the correct answer because it is a class in the Collection Framework that allows storing key-value pairs. It implements the Map interface and provides efficient retrieval and storage of elements based on their keys. HashMap uses a hash table data structure to store the key-value pairs, allowing for fast access and retrieval of elements.
34.
Which are the methods to be overriden to insert user defined type in hashmap?
Correct Answer(s)
B. HashCode()
C. Equals(Object obj)
Explanation
The correct answer is hashCode() and equals(Object obj). These methods need to be overridden in order to insert a user-defined type into a HashMap. The hashCode() method is used to generate a unique hash code for the object, which is used by the HashMap to determine the bucket where the object should be stored. The equals(Object obj) method is used to compare two objects for equality, which is necessary for the HashMap to check if an object already exists in the map. By overriding these methods, the user-defined type can be properly inserted and retrieved from the HashMap.
35.
What is the output of below code snippet?
Map map=new HashMap();
String s1=new String("Key1");
String s2=new String("Key1");
map.put(s1,"Value1");
System.out.println(map.get(s2));
Correct Answer
A. Value1
Explanation
The output of the code snippet will be "Value1". This is because even though the two String objects, s1 and s2, have the same value, they are still different objects in memory. When we use s1 as the key to put a value in the map, it gets stored in the map. When we try to retrieve the value using s2 as the key, it will still return the value "Value1" because the map uses the equals() method to compare keys, not the == operator.
36.
Which is the base class in Exception hierarchy?
Correct Answer
A. Throwable
Explanation
The base class in the Exception hierarchy is Throwable. This class is the superclass for all exceptions and errors in Java. It provides methods and fields for handling and reporting exceptions and errors. The Exception and RuntimeException classes are subclasses of Throwable, but they are not the base class.
37.
What is the output of below code snipet?
final ArrayList list=new ArrayList();
list=new LinkedList()
Correct Answer
B. Compilation error
Explanation
The code snippet will result in a compilation error. This is because the variable "list" is declared as final, which means it cannot be reassigned to a different object. In the code, after declaring "list" as an ArrayList, it is then being reassigned to a LinkedList, which is not allowed due to the final keyword.
38.
What is the output of below code snippet
public class Test {
public static void main(String[] args) {
final int x;
x=11;
final String str=new String("ABC");
str.toLowerCase();
System.out.println(str);
}
}
Correct Answer
B. ABC
Explanation
The code snippet declares a final variable "str" of type String and assigns it the value "ABC". The method "toLowerCase()" is called on the string object, but since strings are immutable in Java, the method does not modify the original string. Therefore, the output of the code will be "ABC".
39.
What is the output of below code snippet?
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args) {
Map map=new HashMap();
map.put(new Integer(1), "one");
map.put(new Integer(1), "two");
System.out.println(map.get(new Integer(1)));
}
}
Correct Answer
C. Two
Explanation
The code snippet creates a HashMap object called "map" and adds two key-value pairs to it. Both keys are Integer objects with the value 1, but they are different instances. The first key-value pair adds the key 1 and the value "one" to the map, and the second key-value pair adds the key 1 and the value "two" to the map. When the code calls the get() method on the map with a new Integer object with the value 1 as the argument, it will retrieve the value associated with that key, which is "two". Therefore, the output of the code snippet will be "two".
40.
Which is the Object class method to be overriden to display meaningfull representation of Object state?
Correct Answer
A. ToString()
Explanation
The correct answer is toString(). This method is used to return a string representation of an object. By overriding this method, we can provide a meaningful representation of the object's state. This can be useful for debugging purposes or for displaying the object in a user-friendly way.
41.
What is the output of the below code snippet
public class Test {
public static void main(String[] args) {
String s1="ABC";
String s2="ABC";
String s3=new String("ABC");
System.out.println((s1==s2)+" "+(s1.equals(s3)));
}
}
Correct Answer
A. True true
Explanation
The output of the code snippet will be "true true" because the == operator checks for reference equality, and since s1 and s2 are both pointing to the same string literal "ABC", they have the same reference. The equals() method, on the other hand, checks for content equality, and since s1 and s3 have the same content "ABC", the result of s1.equals(s3) is also true.
42.
Can interfaces have method implementation?
Correct Answer
B. No, all methods are abstract methods by default
Explanation
Interfaces in most programming languages cannot have method implementations. By default, all methods declared in an interface are abstract methods, meaning they do not have a body. This is because interfaces are meant to define a contract or a set of methods that implementing classes must implement. The actual implementation of these methods is left to the classes that implement the interface. Therefore, the correct answer is that interfaces cannot have method implementations.
43.
In which memory area of JVM objects will be created?
Correct Answer
A. Heap Memory
Explanation
Objects in Java are created in the Heap Memory area of the JVM. The Heap Memory is a region of memory used for dynamic memory allocation, where objects are stored. It is a shared memory area accessible by all threads in a Java application. The JVM automatically manages the allocation and deallocation of memory in the Heap, making it suitable for creating and storing objects. The other memory areas mentioned, such as Stack Memory and Method Area, have different purposes and are not used for object creation.
44.
If there is no memory in Heap,what will happen on before next "new" Statement execution
Correct Answer
B. Garbage collection will run and if memory available new object will be created else OutofMemory is thrown
Explanation
When there is no memory available in the Heap, the garbage collection process will run. If there is enough memory available after garbage collection, a new object will be created. However, if there is still not enough memory available, an OutOfMemoryError will be thrown.
45.
In the below code snippet in which memory area below objects s1 and o1 are created
public class Test {
String s1 =new String("XYZ");
public static void main(String[] args) {
Object o1=new Object();;
}
}
Correct Answer
C. Heap
Explanation
In the given code snippet, the memory area below the objects s1 and o1 are created in the heap. This is because both the String object s1 and the Object object o1 are created using the "new" keyword, which allocates memory in the heap. The stack is used for method calls and local variables, but in this code snippet, there are no method calls or local variables declared. Therefore, the memory area below the objects s1 and o1 is created in the heap.
46.
Which of following is true about Heapmemory configaration
Correct Answer
A. Can be only changed before jvms starts
Explanation
The correct answer is "can be only changed before JVM starts." This means that the configuration of the heap memory in a JVM can only be modified before the JVM is launched. Once the JVM has started, the heap memory configuration cannot be changed dynamically.
47.
What is the entry point of java Execution?
Correct Answer
A. Public static void main(String args[])
Explanation
The correct answer is "public static void main(String args[])". In Java, the main method serves as the entry point for the execution of a program. It is a public method, as it needs to be accessible from outside the class. The "static" keyword is used to allow the method to be called without creating an instance of the class. The return type is "void", indicating that the main method does not return any value. The method takes an array of strings (args) as a parameter, which allows command-line arguments to be passed to the program.
48.
What is the output of below code snippet
public class Test {
static{
System.out.println("1");
}
{
System.out.println("3");
}
public static void main(String[] args) {
System.out.println("2");
}
}
Correct Answer
A. 1 and 2
Explanation
The code snippet includes a static block and an instance block. The static block is executed before the main method, so "1" is printed first. The instance block is executed each time an object of the class is created, so "3" is printed next. Finally, the main method is executed, printing "2". Therefore, the output of the code snippet is "1 and 2".
49.
What is the default date type for whole Numbers and Decimal Numbers?
Correct Answer
B. Int double
Explanation
The default data type for whole numbers and decimal numbers is "int double".
50.
)what is the output of below code snippet
public class Sample {
public static void outcome(int a) {
System.out.print("int ");
}
public static void outcome(long a) {
System.out.print("long ");
}
public static void main(String[] args) {
short shortVar = 1;
long longVar = 109002;
outcome(shortVar);
outcome(longVar);
outcome(1);
}
}
Correct Answer
A. Int long int
Explanation
The code snippet defines three overloaded methods named "outcome". The first method takes an int parameter, the second method takes a long parameter, and the third method takes an int parameter. In the main method, the "outcome" method is called three times with different arguments. The first call passes a short variable, which is automatically promoted to an int, so the first method is called and "int " is printed. The second call passes a long variable, so the second method is called and "long " is printed. The third call passes an int literal, so the third method is called and "int " is printed. Therefore, the output is "int long int".