1.
What all gets printed when the following gets compiled and run?
public class test {
public static void main(String args[]) {
int i=1, j=1;
try {
i++;
j--;
if(i/j > 1)
i++;
}
catch(ArithmeticException e) {
System.out.println(0);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(1);
}
catch(Exception e) {
System.out.println(2);
}
finally {
System.out.println(3);
}
System.out.println(4);
}
}
here
Correct Answer
C. 0 3 4
Explanation
The code will throw an ArithmeticException because of the line "if(i/j > 1)" where j is equal to 0. Since the catch block for ArithmeticException is present, it will execute and print 0. After the catch block, the code will execute the finally block and print 3. Finally, it will print 4. Therefore, the output will be 0 3 4.
2.
What will be the output of the program when to try to execute
public class Arizona {
int id;
String name;
public Arizona() {
this(”aryabhatta”);
System.out.print(”first “);
}
public Arizona(String name) {
this(420, “aryabhatta”);
System.out.print(”second “);
}
public Arizona(int id, String name) {
this.id = id;
this.name = name;
System.out.print(”third “);
}
public static void main(String[] args) {
Arizona b = new Arizona();
System.out.print(b.name +“ “ + b.id);
}
}
Correct Answer
C. Third second first aryabhatta 420
Explanation
The program creates an object of the class Arizona and calls the default constructor. The default constructor then calls the constructor with a String parameter, passing "aryabhatta". The constructor with a String parameter then calls the constructor with an int and a String parameter, passing 420 and "aryabhatta". Finally, the constructor with an int and a String parameter initializes the id and name variables and prints "third".
In the main method, the name and id variables of the Arizona object are printed, which are "aryabhatta" and 420 respectively. Therefore, the output of the program will be "third second first aryabhatta 420".
3.
Consider the following code that contains the class definitions for class ABC, XYZ, and Alphabet. What will be the output of this code?
class ABC extends XYZ
{
ABC () {
super ();
System.out.print (" ABC ");
}
public static void main (String args[])
{
XYZ x1 = new ABC ();
}
}
class XYZ extends Alphabet
{
XYZ ()
{
System.out.print (" XYZ ");
}
}
class Alphabet
{
void Alphabet ()
{
System.out.print (" Alphabet ");
}
}
Correct Answer
D. XYZ ABC
Explanation
The output of this code will be "XYZ ABC". The code creates an object of class ABC, which is a subclass of XYZ. When the object is created, the constructor of class ABC is called, which in turn calls the constructor of class XYZ using the "super" keyword. The constructor of class XYZ then prints "XYZ". Finally, the main method prints "ABC". There is no output for "Alphabet" because the method in class Alphabet is mistakenly named "Alphabet" instead of being a constructor.
4.
What will be the result of compiling and running the following code ?
public class LoopCheck {
public static void main(String args[]) {
int i = 0;
int x = 10;
while ( x > 6 ) {
System.out.print(++i + " ");
x--;
}
}
}
Select the correct answer :
Correct Answer
C. 1 2 3 4
Explanation
The code initializes two variables, i with a value of 0 and x with a value of 10. The while loop condition checks if x is greater than 6. Since x is initially 10, the condition is true and the loop is executed. Inside the loop, the value of i is incremented by 1 and printed, followed by a space. Then, the value of x is decremented by 1. This process continues until x becomes 6. Therefore, the loop will run 4 times, printing the values of i (1, 2, 3, 4) followed by a space each time. Thus, the correct answer is 1 2 3 4.
5.
What all gets printed when the following program is compiled and run. Select the two correct answers.
public class test {
public static void main(String args[]) {
int i, j=1;
i = (j>1)?2:1;
switch(i) {
case 0: System.out.println(0); break;
case 1: System.out.println(1);
case 2: System.out.println(2); break;
case 3: System.out.println(3); break;
}
}
}
Correct Answer
B. 1 2
Explanation
The correct answers are 1 and 2. When the program is run, the value of j is 1, so the value of i is set to 1. The switch statement then checks the value of i. Since i is 1, the case 1 statement is executed and "1" is printed. However, there is no break statement after the case 1 statement, so the program continues to the next case. Since i is 1, the case 2 statement is also executed and "2" is printed. Therefore, the output of the program is "1 2".
6.
________________________ makes Java platform-independent.
Correct Answer
D. Bytecodes
Explanation
Bytecodes are the compiled code instructions that can be executed by the Java Virtual Machine (JVM). The JVM is responsible for interpreting and executing these bytecodes, allowing Java programs to run on any platform that has a JVM installed. This makes Java platform-independent because the same bytecode can be executed on different operating systems without the need for recompilation. Therefore, bytecodes are what make Java programs portable and able to run on any system that supports the JVM.
7.
Byte b = 50 ;
b = b * 2 ;
System.out.println( " b = " + b ) ;
The above fraction of code prints b = 100.
Correct Answer
B. False
Explanation
The code will not print "b = 100" because the expression "b = b * 2" will result in a compilation error. This is because the result of the multiplication operation is an int, and it cannot be automatically converted to a byte without an explicit cast. To fix this, the code should be written as "b = (byte) (b * 2)" to explicitly cast the result to a byte.
8.
Final methods cannot be overridden but overloaded ?
Correct Answer
A. True
Explanation
Final methods in Java cannot be overridden by subclasses. When a method is declared as final, it means that it cannot be modified or overridden by any subclass. However, final methods can still be overloaded, which means that multiple methods with the same name but different parameters can exist in the same class. Overloading allows for different versions of the method to be called depending on the arguments passed to it. Therefore, the statement that final methods cannot be overridden but overloaded is true.
9.
Class Weather
{
static boolean isRaining;
public static void main(String args[])
{
System.out.print(isRaining);
}
}
Correct Answer
B. Prints false
Explanation
The code snippet declares a static boolean variable "isRaining" but does not initialize it. In Java, boolean variables are automatically initialized to false if no value is assigned to them. Therefore, when the main method is called and the code prints the value of "isRaining", it will output false.
10.
What is the name of the method used to start a thread execution?
Correct Answer
C. Start();
Explanation
The correct answer is "start();". In Java, the start() method is used to start the execution of a thread. When start() is called, it creates a new thread and invokes the run() method of that thread. The run() method contains the code that will be executed by the thread. Calling the start() method allows the thread to run concurrently with other threads in the program.
11.
What is the output of following block of program ?
boolean var = false;
if(var = true) {
System.out.println(“TRUE”);
} else {
System.out.println(“FALSE”);
}
Correct Answer
A. Prints TRUE
Explanation
The correct answer is "Prints TRUE". In the if statement, the condition is var = true, which is an assignment statement. The value of var is set to true, and since the assignment statement returns the assigned value, the condition is true. Therefore, the code inside the if block is executed, and "TRUE" is printed.
12.
Which interface provides the capability to store objects using a key-value pair?
Correct Answer
B. Java.util.Map
Explanation
The interface that provides the capability to store objects using a key-value pair is java.util.Map. This interface allows us to associate a unique key with a value, and then retrieve the value using the key. It does not allow duplicate keys, ensuring that each key is unique within the map. Additionally, the Map interface provides various methods to manipulate and access the key-value pairs, such as adding, removing, and updating entries.
13.
What will be the output of the program?
String s = "hello";
Object o = s;
if( o.equals(s) )
{
System.out.println("A");
}
else
{
System.out.println("B");
}
if( s.equals(o) )
{
System.out.println("C");
}
else
{
System.out.println("D");
}
1. A
2. B
3. C
4. D
Correct Answer
D. 1 and 3
Explanation
The output of the program will be "A" and "C". This is because the "equals" method in Java compares the content of the objects, and since the content of both "o" and "s" is "hello", both comparisons will return true. Therefore, the program will print "A" and "C".
14.
What will be the output of the program?
public class X
{
public static void main(String [] args)
{
try
{
badMethod();
System.out.print("A");
}
catch (Exception ex)
{
System.out.print("B");
}
finally
{
System.out.print("C");
}
System.out.print("D");
}
public static void badMethod()
{
throw new Error();
}
}
Correct Answer
D. C is printed before exiting with an error message.
Explanation
The program throws an Error in the badMethod() method. Since the Error is not caught by a catch block for Error, it is propagated to the calling method. The catch block for Exception does not catch Errors, so it is skipped. However, the finally block is always executed, so "C" is printed before the program exits with an error message.
15.
Can there be an abstract class with no abstract methods in it?
Correct Answer
A. Yes
Explanation
Yes, there can be an abstract class with no abstract methods in it. An abstract class is a class that cannot be instantiated and is meant to be subclassed. It can contain both abstract and non-abstract methods. While abstract methods are meant to be overridden by subclasses, non-abstract methods can have their own implementation. So, it is possible for an abstract class to have only non-abstract methods and no abstract methods.
16.
What will be the output of the program?
public class WithoutBook
{
static int x;
boolean catch1()
{
x++;
return true;
}
public static void main(String[] args)
{
if ((catch1() | catch1()) || catch1())
x++;
System.out.println(x);
}
}
Correct Answer
D. Compilation Error
Explanation
The program will result in a compilation error. This is because the variable 'x' is not initialized before it is used in the catch1() method and the main method. Therefore, the compiler will throw an error indicating that the variable 'x' may not have been initialized.
17.
What will be the output of the program?
String s = "ABC";
s.toLowerCase();
s += "def";
System.out.println(s);
Correct Answer
C. ABCdef
Explanation
The program will output "ABCdef".
The variable "s" is initially assigned the value "ABC". The method "toLowerCase()" is called on "s", which converts all the characters in "s" to lowercase. However, the return value of "toLowerCase()" is not assigned to any variable, so the original value of "s" remains unchanged.
Then, the string "def" is concatenated to "s" using the "+=" operator, resulting in "ABCdef".
Finally, the value of "s" is printed using the "println()" method, which outputs "ABCdef" to the console.
18.
What will be the output of the program?
try
{
Float f1 = new Float("3.0");
int x = f1.intValue();
byte b = f1.byteValue();
double d = f1.doubleValue();
System.out.println(x + b + d);
}
catch (NumberFormatException e)
{
System.out.println("bad number");
}
Correct Answer
D. 9.0
Explanation
The program creates a Float object f1 with the value "3.0". The intValue() method is then called on f1, which returns the integer value 3. The byteValue() method is called next, which returns the byte value 3. The doubleValue() method is called last, which returns the double value 3.0. The sum of x, b, and d is then printed, which is 9.0. Since there is no NumberFormatException thrown, the catch block is not executed. Therefore, the output of the program will be 9.0.
19.
What will be the output of the program?
String a = "newspaper";
a = a.substring(5,7);
char b = a.charAt(1);
a = a + b;
System.out.println(a);
Correct Answer
C. App
Explanation
The program first assigns the substring of "newspaper" starting from index 5 and ending at index 7 to the variable 'a'. This results in 'a' being assigned the value "ap". Then, the character at index 1 of 'a' is assigned to the variable 'b', which is 'p'. Next, 'b' is concatenated to 'a', resulting in 'a' being assigned the value "app". Finally, the program prints the value of 'a', which is "app".
20.
What should be at line number 3 to get the total sum of array "sum" ?
public int totalSum( int[] sum ){
int a, b= 0 ;
//which 'for' loop should be here(line :3)
{
b += sum[ a++ ] ;
}
return b ;
}
Correct Answer
D. For( a = 0 ; a< sum.length ; a++ )
Explanation
The correct answer is "for( a = 0 ; a< sum.length ; a++ )". This for loop iterates over each element in the array "sum" and adds it to the variable "b" using the "+=" operator. The variable "a" is initialized to 0 and incremented by 1 in each iteration until it reaches the length of the array "sum". This loop ensures that all elements in the array are added to the variable "b", resulting in the total sum of the array.
21.
Public class Child extends Parent {
public static void main(String[] args) {
Parent p = new Child();
p.method();
}
void method(){
System.out.println("Child method");
}
}
class Parent {
void method(){
System.out.println("Parent method");
}
}
What is the output?
Correct Answer
C. Child method
Explanation
The output of this code will be "Child method". This is because the main method creates an instance of the Child class and assigns it to a variable of type Parent. When the method() is called on this variable, it calls the method defined in the Child class, overriding the method defined in the Parent class. Therefore, the "Child method" is printed to the console.
22.
What will be the output of the program?
public class WBFoo
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
System.out.println( "Finally" );
}
}
}
Correct Answer
A. Finally
Explanation
The program will output "Finally". In the main method, a try block is present with a return statement. However, before the return statement is executed, the finally block is always executed. Therefore, "Finally" will be printed to the console.
23.
Which of the following are true about interfaces. Select the correct answer.
Correct Answer
D. An interface can not extend any number of interfaces.
Explanation
An interface can not extend any number of interfaces means that an interface cannot inherit from multiple interfaces. In Java, a class can implement multiple interfaces, but an interface itself can only extend one interface. This is because interfaces are used to define a contract for classes to implement, and allowing an interface to extend multiple interfaces would create ambiguity and make it difficult to enforce the contract. Therefore, an interface can only extend one interface, if any.
24.
Which collection class allows you to associate its elements with key values, and allows you to retrieve objects in FIFO (first-in, first-out) sequence?
Correct Answer
B. Java.util.LinkedHashMap
Explanation
A LinkedHashMap in Java is a collection class that allows you to associate its elements with key values. It also maintains the order of insertion, allowing you to retrieve objects in FIFO (first-in, first-out) sequence. This means that elements will be returned in the same order they were added to the LinkedHashMap. Therefore, java.util.LinkedHashMap is the correct answer for this question.
25.
Public class CoreJavaOnlineTest1 {
public static void main(String[] args) {
String s1 = "withoutbook";
String s2 = s1;
s1 = null;
System.out.println("s1:"+s1+" s2:"+s2);
}
}
Correct Answer
A. S1:null s2:withoutbook
Explanation
The code initializes a variable s1 with the value "withoutbook" and then assigns the same value to variable s2. After that, s1 is set to null. When the code prints the values of s1 and s2, it will show s1 as null and s2 as "withoutbook". This is because s2 is a separate variable that holds the value "withoutbook" and is not affected by the null assignment to s1.