1.
Which is the correct option about the java interface?
Correct Answer
D. All of the above
Explanation
The correct option about the Java interface is "All of the above." This means that all the statements mentioned in the options are correct. In Java, interfaces are used to achieve multiple inheritances, meaning a class can implement multiple interfaces. An object of an interface cannot be created directly because interfaces cannot be instantiated. However, a class can implement an interface and create objects of that class. Additionally, an interface can extend another interface, allowing for the creation of a hierarchy of interfaces.
2.
In what memory area do variable temp and variable card write in main () get stored?
class CreditCard{
int num;
}
public class Bank {
public static void main(String[] args) {
int temp;
CreditCard card;
}
}
Correct Answer
B. Stack, stack
Explanation
In the given code, the variable "temp" is declared as an integer in the main() method. When a method is called, a new frame is created on the stack to store local variables. So, the variable "temp" will be stored in the stack memory area. Similarly, the variable "card" is declared as an object of the CreditCard class in the main() method. Objects in Java are reference types and the reference variable "card" will also be stored in the stack memory area. Therefore, the correct answer is "Stack, stack".
3.
What is the output of this java program?
public class MemoryJava {
public static void main(String[] args) {
decreaseNumberbyOne(2);
}
public static void decreaseNumberbyOne(int num){
if(num >= 0){
decreaseNumberbyOne(num -1);
}
System.out.println("Number:"+num);
}
}
Correct Answer
A. -1,0,1,2
Explanation
The program recursively calls the `decreaseNumberbyOne` method with `num` decremented by 1 until `num` is less than 0. Then it prints the value of `num`. Since the initial value of `num` is 2, the program will call the method with values 1, 0, and -1 before printing the value of -1. Therefore, the output will be -1, 0, 1, 2.
4.
Which polymorphism behavior do you see in the below class?
class Paint {
// all methods have same name
public void Color(int x) {
}
public void Color(int x, int y) {
}
public void Color(int x, int y, int z) {
}
}
Correct Answer
A. Method overloading
Explanation
The given class demonstrates method overloading. Method overloading occurs when multiple methods in a class have the same name but different parameters. In this case, the "Color" method is defined three times with different numbers of parameters. This allows the class to provide different ways of coloring an object based on the number of arguments passed to the method.
5.
__________ can be used to control the order of certain data structures and collection of objects too.
Correct Answer
C. Comparators
Explanation
Comparators can be used to control the order of certain data structures and collections of objects. Comparators provide a way to define custom ordering for objects, allowing for sorting and ordering based on specific criteria. By implementing the Comparator interface, developers can define their own comparison logic and apply it to various data structures and collections, giving them control over the ordering of the elements. Therefore, the correct answer is "Comparators".
6.
In the below java code, whose “Car” will be called?
class Father {
public void car() {
System.out.println("Father's Car");
}
}
class Son extends Father {
public void car() {
System.out.println("Son's Car");
}
}
public class Sample {
public static void main(String[] args) {
Son john = new Son();
john.car();
}
}
Correct Answer
B. Son’s Car
Explanation
In this Java code, the method `car()` is overridden in the `Son` class, which means that when the `car()` method is called on the object `john`, it will execute the code inside the `car()` method in the `Son` class. Therefore, the output will be "Son's Car".
7.
What exception can occur in the below java program if we access 5 element in the array that does not exist?
public class TException {
public static void main(String[] args) {
try {
int a[] = { 5, 10, 15, 20 };
System.out.println("Element :" + a[4]);
}
finally{}
}
}
Correct Answer
A. ArrayIndexOutOfBoundsException
Explanation
If we try to access the 5th element in the array using the index 4 (a[4]), it will result in an ArrayIndexOutOfBoundsException. This exception occurs when we try to access an index that is outside the bounds of the array. In this case, the array has only 4 elements (indexes 0, 1, 2, and 3), so trying to access the 5th element will throw this exception.
8.
After the following code fragment, what is the value in fname?
String str; int fname; str = “Foolish boy.”; fname = str.indexOf(“fool”);
Correct Answer
C. -1
Explanation
The value in fname is -1 because the method indexOf() returns the index of the first occurrence of the specified substring in the given string. In this case, the substring "fool" is not found in the string "Foolish boy." so the method returns -1.
9.
Direct subclass of Throwable in Java
Correct Answer
C. Both A & C
Explanation
Both A and C are correct because in Java, both Exception and Error are direct subclasses of Throwable. This means that both Exception and Error inherit directly from the Throwable class, making them direct subclasses.
10.
Given the following code snippet;
int salaries[]; int index = 0; salaries = new int salaries[4]; while (index < 4) { salaries[index] = 10000; index++; } What is the value of salaries[3]?
Correct Answer
D. 10000
Explanation
The code snippet initializes an array called "salaries" with a size of 4. Then, it enters a while loop that iterates as long as the "index" variable is less than 4. Inside the loop, it assigns the value of 10000 to each element of the "salaries" array, starting from index 0 and incrementing the index by 1 each time. Therefore, the value of salaries[3] is 10000.
11.
Assume that the value 3929.92 is of type float. How to assign this value after declaring the variable interest of type float?
Correct Answer
D. Interest = 3929.92f
Explanation
To assign the value 3929.92 to the variable interest of type float, we use the syntax interest = 3929.92f. The "f" at the end of the number indicates that it is a float value.
12.
What is the data type for the number 9.6352?
Correct Answer
B. Double
Explanation
The data type for the number 9.6352 is double. This is because double is a data type that can hold decimal values with a higher precision compared to float.
13.
In a class definition, the special method provided to be called to create an instance of that class is known as a/an
Correct Answer
C. Constructor
Explanation
In a class definition, the special method provided to be called to create an instance of that class is known as a constructor. Constructors are used to initialize the object's state and allocate memory for the object. They are typically defined with the same name as the class and are automatically called when an object of the class is created.
14.
What is the meaning of the return data type void?
Correct Answer
B. Void returns no data type.
Explanation
The return data type "void" is used in programming languages to indicate that a function or method does not return any value. It is used when the function or method is intended to perform a task or operation without producing a result that needs to be stored or used elsewhere in the program. In other words, when a function or method has a return type of "void", it means that it does not return any data or memory space for the developers to utilize.
15.
Java object oriented programming concepts is/are
Correct Answer
D. All of the above.
Explanation
The correct answer is "All of the above" because Java object-oriented programming concepts include encapsulation, inheritance, and polymorphism. Encapsulation refers to the bundling of data and methods together into a single unit, known as a class. Inheritance allows classes to inherit properties and methods from other classes, creating a hierarchy of classes. Polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling flexibility and code reusability. Therefore, all three concepts are fundamental to Java's object-oriented programming paradigm.