1.
Consider the following code and choose the correct option:
class X {
int x;
X(int x){
x=2;
}
}
class Y extends X{
Y(){}
void displayX(){
System.out.print(x);
}
public static void main(String args[]){
new Y().displayX();
}
}
Correct Answer
D. Compilation error
Explanation
The code will result in a compilation error because the variable "x" in the constructor of class X is not properly assigned. The statement "x=2;" should be changed to "this.x=2;" to correctly assign the value to the instance variable "x". Without this change, the variable "x" remains uninitialized and will have a default value of 0, which will be displayed in the displayX() method.
2.
Consider the following code and choose the correct option:
class Test{
private void display(){
System.out.println("Display()");}
private static void show() {
display();
System.out.println("show()");
}
public static void main(String arg[]){
show();
}
}
Correct Answer
D. Compilation error
Explanation
The code will result in a compilation error because the method "display()" is declared as private and can only be accessed within the same class. Since the method "show()" is a static method and is trying to call the private method "display()", it is not allowed and will result in a compilation error.
3.
Consider the following code and choose the correct option:
class A{
A(){
System.out.print("From A");
}
}
class B extends A{
B(int z){
z=2;
}
public static void main(String args[]){
new B(3);
}
}
Correct Answer
B. Comiples and prints From A
Explanation
The code compiles and prints "From A" because the constructor of class A is called when an object of class B is created. The constructor of class B does not have any print statements, so only the print statement in the constructor of class A is executed.
4.
Consider the following code and choose the correct option:
class One{
int var1;
One (int x){
var1 = x;
}
}
class Derived extends One{
int var2;
Derived(){
super(1);
}
void display(){
System.out.println("var 1="+var1+", var 2="+var2);
}
}
class Main{
public static void main(String[] args){
Derived obj = new Derived();
obj.display();
}
}
Correct Answer
B. Var 1=1 var 2=0
Explanation
The code compiles successfully because there are no syntax errors. However, at runtime, the program will encounter a NullPointerException when trying to access the variable var2 in the display() method. This is because var2 has not been initialized and has a default value of 0. Therefore, the output will be "var 1=1 var 2=0".
5.
Consider the following code and choose the correct option:
class Order{
Order(){
System.out.println("Cat");
}
public static void main(String... Args){
Order obj = new Order();
System.out.println("Ant");
}
static{
System.out.println("Dog");
}
{
System.out.println("Man");
}
}
Correct Answer
C. Dog Man Cat Ant
Explanation
The correct answer is "Dog Man Cat Ant". This is because the static block is executed first, which prints "Dog". Then, the instance block is executed, which prints "Man". After that, the constructor is called, which prints "Cat". Finally, in the main method, "Ant" is printed.
6.
Consider the following code and choose the correct option:
public class MyAr {
public static void main(String argv[]) {
MyAr m = new MyAr();
m.amethod();
}
public void amethod() {
final int i1;
System.out.println(i1);
}
}
Correct Answer
A. Unresolved compilation problem: The local variable i1 may not have been initialized
Explanation
The code is attempting to print the value of the local variable "i1" without initializing it first. This results in a compilation error because local variables must be assigned a value before they can be used. Therefore, the correct answer is "Unresolved compilation problem: The local variable i1 may not have been initialized."
7.
What would be the output?
class MyClass1
{
private int area(int side)
{
return(side * side);
}
public static void main(String args[ ])
{
MyClass1 MC = new MyClass1( );
int area = MC.area(50);
System.out.println(area);
}
}
Correct Answer
C. 2500
Explanation
The code defines a class MyClass1 with a private method area that takes an integer parameter side and returns the square of the side. In the main method, an object of MyClass1 is created and the area method is called with a parameter value of 50. The returned value is stored in a variable area and then printed. Since the area method correctly calculates the square of the side, the output will be 2500.
8.
Consider the following code and choose the correct option:
public class MyClass {
public static void main(String arguments[]) {
amethod(arguments);
}
public static void amethod(String[] arguments) {
System.out.println(arguments[0]);
System.out.println(arguments[1]);
}
}
Correct Answer
D. Runtime Error
Explanation
The given code is trying to access elements from the "arguments" array in the amethod() method. However, the code does not check if the array has at least two elements before accessing them. Therefore, if the "arguments" array does not have at least two elements, it will result in a runtime error.
9.
Examine this code:
String stringA = "Wild";
String stringB = " Irish";
String stringC = " Rose";
String result;
Which of the following puts a reference to "Wild Irish Rose" in result?
Correct Answer
A. Result = stringA.concat( stringB.concat( stringC ) );
Explanation
The correct answer is `result = stringA.concat( stringB.concat( stringC ) );` because it concatenates `stringB` and `stringC` first using the `concat()` method, and then concatenates the result with `stringA` using the `concat()` method again. This creates the string "Wild Irish Rose" and assigns it to the `result` variable.
10.
Carefully read the question and answer accordingly.
A class can be declared as _______ if you do not want the class to be subclasses.
Using the __________keyword we can abstract a class from its implementation
Correct Answer
B. Final, interface
Explanation
A class can be declared as "final" if you do not want the class to be subclasses. Using the "interface" keyword we can abstract a class from its implementation.
11.
Consider the following code and choose the correct option:
abstract class MineBase {
abstract void amethod();
static int i;
}
public class Mine extends MineBase {
public static void main(String argv[]){
int[] ar=new int[5];
for(i=0;i < ar.length;i++)
System.out.println(ar[i]);
}
}
Correct Answer
D. Compilation Error occurs and to avoid them we need to declare Mine class as abstract
Explanation
The correct answer is that a compilation error occurs and to avoid it, the Mine class needs to be declared as abstract. This is because the Mine class is inheriting from the abstract class MineBase, which has an abstract method amethod(). In order for a class to inherit from an abstract class with abstract methods, the inheriting class must either provide an implementation for the abstract method or itself be declared as abstract. Since the Mine class does not provide an implementation for amethod(), it needs to be declared as abstract to avoid the compilation error.
12.
What will be the result when you attempt to compile this program?
public class Rand{
public static void main(String argv[]){
int iRand;
iRand = Math.random();
System.out.println(iRand);
}
}
Correct Answer
A. Compile time error referring to a cast problem
Explanation
The program will result in a compile time error referring to a cast problem. This is because the Math.random() method returns a double value between 0.0 and 1.0, and you are trying to assign it to an integer variable iRand without casting it. To fix this error, you should explicitly cast the double value to an integer using (int) before assigning it to iRand.
13.
Which of the following declarations are correct? (Choose TWO)
Correct Answer(s)
C. String s = “null”;
D. Int i = new Integer(“56”);
Explanation
The correct answer is "String s = 'null';, int i = new Integer('56');". The first declaration is correct because it assigns the string value "null" to the variable s. The second declaration is correct because it creates a new Integer object with the value 56 and assigns it to the variable i. The other two declarations are incorrect. The first one should use lowercase 'true' instead of 'TRUE' to assign a boolean value, and the second one is incorrect because the byte data type can only hold values from -128 to 127, so assigning 256 is out of range.
14.
Class A, B and C are in multilevel inheritance hierarchy respectively. In the main method of some other class if class C object is created, in what sequence the three constructors execute?
Correct Answer
A. Constructor of A executes first, followed by the constructor of B and C
Explanation
In a multilevel inheritance hierarchy, the constructors of the classes are executed in a specific order. In this case, since class C is the most derived class, its constructor will execute first. After that, the constructor of class B, which is the immediate parent of class C, will execute. Finally, the constructor of class A, which is the parent of class B, will execute. Therefore, the correct sequence of constructor execution is: Constructor of C executes first, followed by the constructor of B and A.
15.
Examine this code:
class Base{
Base(){
int i = 100;
System.out.println(i);
}
}
public class Pri extends Base{
static int i = 200;
public static void main(String argv[]){
new Base();
System.out.println(i);
}
}
Correct Answer
B. 100 200
Explanation
The code creates a class called Base with a constructor that initializes a local variable i with the value 100 and prints it. Then, a class called Pri extends Base and has a static variable i with the value 200. In the main method, a new instance of Base is created, which calls the constructor and prints 100. Then, the value of the static variable i is printed, which is 200. Therefore, the output will be "100 200".
16.
Given,
public class Q {
public static void main(String argv[]) {
int anar[] = new int[] { 1, 2, 3 };
System.out.println(anar[1]);
}
}
Correct Answer
B. 2
Explanation
The correct answer is 2 because the code initializes anar array with values {1, 2, 3} and then prints the value at index 1, which is 2.
17.
Which statements, when inserted at (1), will not result in compile-time errors?
[Choose Three]
public class ThisUsage {
int planets;
static int suns;
public void gaze() {
int i;
// (1) INSERT STATEMENT HERE
}
}
Correct Answer(s)
A. I = this.planets;
B. I = this.suns;
E. This.suns = planets;
Explanation
The statements "i = this.planets;" and "i = this.suns;" will not result in compile-time errors because "this" refers to the current instance of the class, and "planets" and "suns" are instance variables of the class. Therefore, it is valid to assign the values of these instance variables to the local variable "i".
The statement "this.suns = planets;" will also not result in a compile-time error because "this.suns" refers to the static variable "suns" in the class, and "planets" is an instance variable. Assigning the value of the instance variable "planets" to the static variable "suns" is valid.
The other statements "this = new ThisUsage();" and "this.i = 4;" will result in compile-time errors. Assigning a new instance of the class to "this" is not allowed, and there is no instance variable "i" in the class.
18.
Given the following code what will be output?
public class Pass{
static int j=20;
public static void main(String argv[]){
int i=10;
Pass p = new Pass();
p.amethod(i);
System.out.println(i);
System.out.println(j);
}
public void amethod(int i) {
j+=(i+i);
}
}
Correct Answer
B. 10 and 40
Explanation
The code will output "10 and 40". In the main method, the variable i is initialized to 10. Then, a new instance of the Pass class is created and the amethod() method is called, passing in the value of i. In the amethod() method, the value of j is incremented by (i+i), which is 20. Then, in the main method, the value of i is printed, which is still 10. Finally, the value of j is printed, which is now 40.
19.
Given,
public class c123 {
private c123() {
System.out.println("Hellow");
}
public static void main(String args[]) {
c123 o1 = new c123();
c213 o2 = new c213();
}
}
class c213 {
private c213() {
System.out.println("Hello123");
}
}
Correct Answer
C. Compilation Error
Explanation
The given code will result in a compilation error. This is because the constructor of the class c123 is declared as private, which means it cannot be accessed outside of the class. In the main method, when trying to create an object of c123 using the new keyword, it will throw a compilation error because the constructor is not accessible.
20.
Here is the general syntax for method definition:
accessModifier returnType methodName( parameterList )
{
Java statements
return returnValue;
}
What is true for the returnType and the returnValue?
Correct Answer
C. The returnValue must be the same type as the returnType, or be of a type that can be converted to returnType without loss of information
Explanation
The explanation for the given correct answer is that the returnType in a method definition specifies the type of value that the method will return. The returnValue, on the other hand, is the actual value that is returned by the method. According to the given answer, the returnValue must be the same type as the returnType or a type that can be converted to returnType without losing any information. This means that the method can return a value of the same type as returnType, or a value of a type that is compatible with returnType.