UUUU
1.What will be the output of the program?
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
A.finished
B.Exception
C.Compilation fails.
D.Arithmetic Exception
Your Answer: Option C
Correct Answer: Option C
Explanation:
Compilation fails because ArithmeticException has already been caught. ArithmeticException is a subclass of
java.lang.Exception, by time the ArithmeticException has been specified it has already been caught by the Exception class.
If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more
specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses).
2.What is the most restrictive access modifier that will allow members of one class to have access to members of
another class in the same package?
A.public
B.abstract
C.protected
D.synchronized
E.default access
Your Answer: Option E
Correct Answer: Option E
Explanation:
default access is the "package oriented" access modifier.
Option A and C are wrong because public and protected are less restrictive. Option B and D are wrong because
abstract and synchronized are not access modifiers.
3.
class Test
{
private Demo d;
void start()
{
d = new Demo();
this.takeDemo(d); /* Line 7 */
} /* Line 8 */
void takeDemo(Demo demo)
{
demo = null;
demo = new Demo();
}
}
When is the Demo object eligible for garbage collection?
A.After line 7
B.After line 8
C.After the start() method completes
D.When the instance running this code is made eligible for garbage collection.
Your Answer: Option (Not Answered)
Correct Answer: Option D
Explanation:
Option D is correct. By a process of elimination.
Option A is wrong. The variable d is a member of the Test class and is never directly set to null.
Option B is wrong. A copy of the variable d is set to null and not the actual variable d. Option C is wrong. The variable d exists outside the start() method (it is a class member). So, when the start()
method finishes the variable d still holds a reference.
Learn more problems on : Garbage Collections
Discuss about this problem : Discuss in Forum
4.Which two of the following methods are defined in class Thread?
1. start()
2. wait()
3. notify()
4. run()
5. terminate()
A.1 and 4
B.2 and 3
C.3 and 4
D.2 and 4
Your Answer: Option (Not Answered)
Correct Answer: Option A
Explanation:
(1) and (4). Only start() and run() are defined by the Thread class.
(2) and (3) are incorrect because they are methods of the Object class. (5) is incorrect because there's no such
method in any thread-related class.
Learn more problems on : Threads
Discuss about this problem : Discuss in Forum
5.What is the name of the method used to start a thread execution?
A.init();
B.start();
C.run();
D.resume();
Your Answer: Option (Not Answered)
Correct Answer: Option B
Explanation:
Option B is Correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the
run method of this thread.
Option A is wrong. There is no init() method in the Thread class.
Option C is wrong. The run() method of a thread is like the main() method to an application. Starting the thread
causes the object's run method to be called in that separately executing thread.
Option D is wrong. The resume() method is deprecated. It resumes a suspended thread.
Learn more problems on : Threads
Discuss about this problem : Discuss in Forum
6.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);
A.apa
B.app
C.apea
D.apep
Your Answer: Option B
Correct Answer: Option B
Explanation:
Both substring() and charAt() methods are indexed with a zero-base, and substring() returns a String of length
arg2 - arg1. Learn more problems on : Java.lang Class
Discuss about this problem : Discuss in Forum
7. What will be the output of the program?
class Tree { }
class Pine extends Tree { }
class Oak extends Tree { }
public class Forest1
{
public static void main (String [] args)
{
Tree tree = new Pine();
if( tree instanceof Pine )
System.out.println ("Pine");
else if( tree instanceof Tree )
System.out.println ("Tree");
else if( tree instanceof Oak )
System.out.println ( "Oak" );
else
System.out.println ("Oops ");
}
}
A.Pine
B.Tree
C.Forest
D.Oops
Your Answer: Option A
Correct Answer: Option A
Explanation:
The program prints "Pine".
Learn more problems on : Java.lang Class
Discuss about this problem : Discuss in Forum
8.What will be the output of the program?
String x = "xyz";
x.toUpperCase(); /* Line 2 */
String y = x.replace('Y', 'y');
y = y + "abc";
System.out.println(y);
A.abcXyZ
B.abcxyz
C.xyzabc
D.XyZabc
Your Answer: Option C
Correct Answer: Option C
Explanation:
Line 2 creates a new String object with the value "XYZ", but this new object is immediately lost because there is no
reference to it. Line 3 creates a new String object referenced by y. This new String object has the value "xyz" because there was no "Y" in the String object referred to by x. Line 4 creates a new String object, appends "abc" to
the value "xyz", and refers y to the result.
Learn more problems on : Java.lang Class
Discuss about this problem : Discuss in Forum
9.What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x=20;
String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge";
System.out.println(sup);
}
}
A.small
B.tiny
C.huge
D.Compilation fails
Your Answer: Option B
Correct Answer: Option B
Explanation:
This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is
assigned to sup. Learn more problems on : Operators and Assignments
Discuss about this problem : Discuss in Forum
10.The following block of code creates a Thread using a Runnable target:
Runnable target = new MyRunnable();
Thread myThread = new Thread(target);
Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
A.public class MyRunnable extends Runnable{public void run(){}}
B.public class MyRunnable extends Object{public void run(){}}
C.public class MyRunnable implements Runnable{public void run(){}}
D.public class MyRunnable implements Runnable{void run(){}}
Your Answer: Option (Not Answered)
Correct Answer: Option C
Explanation:
The class correctly implements the Runnable interface with a legal public void run() method.
Option A is incorrect because interfaces are not extended; they are implemented.
Option B is incorrect because even though the class would compile and it has a valid public void run() method, it
does not implement the Runnable interface, so the compiler would complain when creating a Thread with an
instance of it.
Option D is incorrect because the run() method must be public. Learn more problems on : Threads
Discuss about this problem : Discuss in Forum
11.Which of the following statements is true?
A.In an assert statement, the expression after the colon ( : ) can be any Java expression.
B.If a switch block has no default, adding an assert default is considered appropriate.
C.In an assert statement, if the expression after the colon ( : ) does not have a value, the assert's error
message will be empty.
D.It is appropriate to handle assertion failures using a catch clause.
Test Result https://www.indiabix.com/online-test/java-programming-test/random
6 of 12 11-03-2020, 05:44
Your Answer: Option (Not Answered)
Correct Answer: Option B
Explanation:
Adding an assertion statement to a switch statement that previously had no default case is considered an excellent
use of the assert mechanism.
Option A is incorrect because only Java expressions that return a value can be used. For instance, a method that
returns void is illegal.
Option C is incorrect because the expression after the colon must have a value.
Option D is incorrect because assertions throw errors and not exceptions, and assertion errors do cause program
termination and should not be handled.
Learn more problems on : Assertions
Discuss about this problem : Discuss in Forum
12.What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread();
Thread x = new Thread(t);
x.start(); /* Line 7 */
}
public void run()
{
for(int i = 0; i < 3; ++i)
{
System.out.print(i + "..");
}
}
}
A.Compilation fails.
B.1..2..3..
C.0..1..2..3..
D.0..1..2..
Your Answer: Option (Not Answered)
Correct Answer: Option D
Explanation:
The thread MyThread will start and loop three times (from 0 to 2).
Option A is incorrect because the Thread class implements the Runnable interface; therefore, in line 7, Thread
can take an object of type Thread as an argument in the constructor.
Option B and C are incorrect because the variable i in the for loop starts with a value of 0 and ends with a value
of 2.
Learn more problems on : Threads
Discuss about this problem : Discuss in Forum
13.Which collection class allows you to access its elements by associating a key with an element's value, and
provides synchronization?
A.java.util.SortedMap
B.java.util.TreeMap
C.java.util.TreeSet
D.java.util.Hashtable
Your Answer: Option (Not Answered)
Correct Answer: Option D
Explanation:
Hashtable is the only class listed that provides synchronized methods. If you need synchronization great;
otherwise, use HashMap, it's faster.
Learn more problems on : Objects and Collections
Discuss about this problem : Discuss in Forum
14.What will be the output of the program?
public class Test
{
public static void main (String args[])
{
String str = NULL;
System.out.println(str);
}
}
A.NULL
B.Compile Error
C.Code runs but no output
D.Runtime Exception
Your Answer: Option (Not Answered)
Correct Answer: Option B
Explanation:
Option B is correct because to set the value of a String variable to null you must use "null" and not "NULL".
Learn more problems on : Objects and Collections
Discuss about this problem : Discuss in Forum
15.Which one of these lists contains only Java programming language keywords?
A.class, if, void, long, Int, continue
B.goto, instanceof, native, finally, default, throws
C.try, virtual, throw, final, volatile, transient
D.strictfp, constant, super, implements, do
E.byte, break, assert, switch, include
Your Answer: Option (Not Answered)
Correct Answer: Option B
Explanation:
All the words in option B are among the 49 Java keywords. Although goto reserved as a keyword in Java, goto is
not used and has no function.
Option A is wrong because the keyword for the primitive int starts with a lowercase i.
Option C is wrong because "virtual" is a keyword in C++, but not Java.
Option D is wrong because "constant" is not a keyword. Constants in Java are marked static and final. Option E is wrong because "include" is a keyword in C, but not in Java.
Learn more problems on : Language Fundamentals
Discuss about this problem : Discuss in Forum
16.Which three are valid declarations of a char?
1. char c1 = 064770;
2. char c2 = 'face';
3. char c3 = 0xbeef;
4. char c4 = \u0022;
5. char c5 = '\iface';
6. char c6 = '\uface';
A.1, 2, 4
B.1, 3, 6
C.3, 5
D.5 only
Your Answer: Option (Not Answered)
Correct Answer: Option B
Explanation:
(1), (3), and (6) are correct. char c1 = 064770; is an octal representation of the integer value 27128, which is
legal because it fits into an unsigned 16-bit integer. char c3 = 0xbeef; is a hexadecimal representation of the
integer value 48879, which fits into an unsigned 16-bit integer. char c6 = '\uface'; is a Unicode representation of
a character.
char c2 = 'face'; is wrong because you can't put more than one character in a char literal. The only other
acceptable char literal that can go between single quotes is a Unicode value, and Unicode literals must always
start with a '\u'. char c4 = \u0022; is wrong because the single quotes are missing.
char c5 = '\iface'; is wrong because it appears to be a Unicode representation (notice the backslash), but starts
Test Result https://www.indiabix.com/online-test/java-programming-test/random
9 of 12 11-03-2020, 05:44
with '\i' rather than '\u'. Learn more problems on : Language Fundamentals
Discuss about this problem : Discuss in Forum
17. Which one is a valid declaration of a boolean?
A.boolean b1 = 0;
B.boolean b2 = 'false';
C.boolean b3 = false;
D.boolean b4 = Boolean.false();
E.boolean b5 = no;
Your Answer: Option (Not Answered)
Correct Answer: Option C
Explanation:
A boolean can only be assigned the literal true or false. Learn more problems on : Language Fundamentals
Discuss about this problem : Discuss in Forum
18.
class Test1
{
public int value;
public int hashCode() { return 42; }
}
class Test2
{
public int value;
public int hashcode() { return (int)(value^5); }
}
which statement is true?
A.class Test1 will not compile.
B.The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
C.The Test1 hashCode() method is less efficient than the Test2 hashCode() method.
D.class Test2 will not compile.
Your Answer: Option (Not Answered)
Correct Answer: Option C
Explanation:
The so-called "hashing algorithm" implemented by class Test1 will always return the same value, 42, which is
legal but which will place all of the hash table entries into a single bucket, the most inefficient setup possible.
Option A and D are incorrect because these classes are legal.
Option B is incorrect based on the logic described above.
Learn more problems on : Objects and Collections
Discuss about this problem : Discuss in Forum
19.Which two statements are equivalent?
1. 3/2
2. 3<2
3. 3*4
4. 3<<2
A.1 and 2
B.2 and 3
C.3 and 4
D.1 and 4
Your Answer: Option (Not Answered)
Correct Answer: Option C
Explanation:
(1) is wrong. 3/2 = 1 (integer arithmetic).
(2) is wrong. 3 < 2 = false. (3) is correct. 3 * 4 = 12. (4) is correct. 3 <<2= 12. In binary 3 is 11, now shift the bits two places to the left and we get 1100 which is 12
in binary (3*2*2). Learn more problems on : Operators and Assignments
Discuss about this problem : Discuss in Forum
20.Which of the following are legal lines of code?
1. int w = (int)888.8;
2. byte x = (byte)1000L;
3. long y = (byte)100;
4. byte z = (byte)100L;
A.1 and 2
B.2 and 3
C.3 and 4
D.All statements are correct.
Your Answer: Option (Not Answered)
Correct Answer: Option D
Explanation:
Statements (1), (2), (3), and (4) are correct. (1) is correct because when a floating-point number (a double in this
case) is cast to an int, it simply loses the digits after the decimal.
(2) and (4) are correct because a long can be cast into a byte. If the long is over 127, it loses its most significant