Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Friday, August 21, 2009

Java Quiz -8

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

1)I have class called GlobalCache in package com.planetsoft.cache.
I want to print the name of the class, without creating an object
of the class.


Ans: GlobalCache.class.getName()

2)What is the output of the following program?

System.out.println("value=" + 08);


Ans: Compilation Error: Octal digit out of range

3)What is the output of the following code

private void display()
{
int ret = catchMe(true);
System.out.println("ret=" + ret);

ret = catchMe(false);
System.out.println("ret=" + ret);

}
private int catchMe(boolean cond)
{

try
{
if (cond) throw new Exception("catch me");
return 1;
}
catch(Exception ex)
{
return 2;
}
finally
{
return 3;
}

}

Ans:

ret=3
ret=3


5)What is wrong in the following code? suggest a solution to fix it

public static void display()
{
final long MICROS_PER_DAY = 24 * 60 * 60 * 1000 * 1000;
final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
}

Ans:

The problem is arithmetic overflow. Solution is to prefix the
numbers with L, to make the computation in long arithmetic.


6)Mr. Jack has a 15cm gold bar and wants to give Mr. Jill, 1 cm
of gold bar on day 1, 2cms of gold bar on day 2, and so on..., .Finally
Mr. Jack should give away 15 cms of gold bar on day 15.
Following are the rules
a)Mr. Jack can make at the most 3 cuts in the gold bar
b)Mr. Jack can take back the gold bars , given to Mr. Jill, on any
given day.

Ans:
Make 3 cuts at 1,2 and 4 cms

7)A car runs for 10,000 kms. It has one stepney(i.e, extra tyre).
If all tyres has to be used equally, what is the total no. of
kms, each tyre would have run?

Ans:
8000 kms

8)There are 3 fruit baskets - one with full of Apples, one with
full of Oranges and one with both apples and oranges. All boxes
are labelled wrongly. You cannot see what is inside the box.
Given these , you have to pick only one fruit from any one of the
box of your choice, and label the boxes correctly. How do you
solve this?

Ans:

----
Pick one fruit from Box labelled "Mixed"
If it is Apple then

Rename "Mixed" as "Apple".
Rename "Apple" as "Orange"
Rename "Orange" as "Mixed"

else If it is Orange then

Rename "Mixed" as "Orange".
Rename "Orange" as "Apple"
Rename "Apple" as "Mixed"

End if

9)From a List of String , how to get array of Strings
Note: You only have to fill up the blanks in the following code.

private static void display()
{
List l = new ArrayList();

l.add("tom");
l.add("harry");
String [] arr = ------------------;

}

Ans:
(String [])l.toArray(new String[0])


10)What is the output of the following method

private static void display()
{
char x = 'X';
int i = 0;
System.out.print(true ? x : 0);
System.out.print(false ? i : x);

}

Ans:
X88

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible!

No comments: