Java Questions and Answers Part-27

1. What will be the output of the following Java program?
class exception_handling
{
public static void main(String args[])
{
try
{
int a[] = {1, 2,3 , 4, 5};
for (int i = 0; i < 7; ++i)
System.out.print(a[i]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("0");
}
}
}
a) 12345
b) 123450
c) 1234500
d) Compilation Error

Answer: b
Explanation: When array index goes out of bound then ArrayIndexOutOfBoundsException exception is thrown by the system.
Output:
123450

2. What will be the output of the following Java program?
class exception_handling
{
public static void main(String args[])
{
try
{
int a[] = {1, 2,3 , 4, 5};
for (int i = 0; i < 5; ++i)
System.out.print(a[i]);
int x = 1/0;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("A");
}
catch(ArithmeticException e)
{
System.out.print("B");
}
}
}
a) 12345
b) 12345A
c) 12345B
d) Compilation Error

Answer: c
Explanation: There can be more than one catch of a single try block. Here Arithmetic exception occurs instead of Array index out of bound exception hence B is printed after 12345
Output:
12345B

3. What will be the output of the following Java program?
class exception_handling
{
static void throwexception() throws ArithmeticException
{
System.out.print("0");
throw new ArithmeticException ("Exception");
}
public static void main(String args[])
{
try
{
throwexception();
}
catch (ArithmeticException e)
{
System.out.println("A");
}
}
}
a) A
b) 0
c) 0A
d) Exception

Answer: c
Explanation:
Output:
0A

4. What will be the output of the following Java program?
class exception_handling
{
public static void main(String args[])
{
try
{
int a = 1;
int b = 10 / a;
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c[] = {1};
c[8] = 9;
}
finally
{
System.out.print("A");
}
}
catch (NullPointerException e)
{
System.out.println("B");
}
}
}
a) A
b) B
c) AB
d) BA

Answer: a
Explanation: The inner try block does not have a catch which can tackle ArrayIndexOutOfBoundException hence finally is executed which prints ‘A’ the outer try block does have catch for NullPointerException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
Output:
A

5. What will be the output of the following Java program?
class exception_handling
{
public static void main(String args[])
{
try
{
int a = args.length;
int b = 10 / a;
System.out.print(a);
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
System.out.println("TypeA");
}
catch (ArithmeticException e)
{
System.out.println("TypeB");
}
}
}
a) TypeA
b) TypeB
c) 0TypeA
d) 0TypeB

Answer: d
Explanation: Execution command line is “$ java exception_ handling one two” hence there are two input making args.length = 2, hence “c[8] = 9” in second try block is executing which throws ArrayIndexOutOfBoundException which is caught by catch of nested try block. Hence 0TypeB is printed.
Output:
0TypeB

6. Which of these class provides various types of rounding functions?
a) Math
b) Process
c) System
d) Object

Answer: a
Explanation: Math

7. Which of these methods return a smallest whole number greater than or equal to variable X?
a) double ceil(double X)
b) double floor(double X)
c) double max(double X)
d) double min(double X)

Answer: a
Explanation: ceil(double X) returns the smallest whole number greater than or equal to variable X.

8. Which of these method returns a largest whole number less than or equal to variable X?
a) double ceil(double X)
b) double floor(double X)
c) double max(double X)
d) double min(double X)

Answer: b
Explanation: double floor(double X) returns a largest whole number less than or equal to variable X.

9. Which of function return absolute value of a variable?
a) abs()
b) absolute()
c) absolutevariable()
d) none of the mentioned

Answer: a
Explanation: abs() returns the absolute value of a variable.

10. What will be the output of the following Java code?
class A
{
int x;
int y;
void display()
{
System.out.print(x + " " + y);
}
}
class Output
{
public static void main(String args[])
{
A obj1 = new A();
A obj2 = new A();
obj1.x = 1;
obj1.y = 2;
obj2 = obj1.clone();
obj1.display();
obj2.display();
}
}
a) 1 2 0 0
b) 1 2 1 2
c) 0 0 0 0
d) System Dependent

Answer: b
Explanation: clone() method of object class is used to generate duplicate copy of the object on which it is called. Copy of obj1 is generated and stored in obj2.
Output:
1 2 1 2