Core Java Basic Level I Question Bank
Core Java Basic Level I Question Bank
1) Which of the following option leads to the portability and security of Java?
a. Bytecode is executed by JVMb.
b. The applet makes the Java code secure and portable
c. Use of exception handling
d. Dynamic binding between objects
Answer: A
1 powered by SPPUQuestionPapers.com
4) The \u0021 article referred to as a
a. Unicode escape sequence
b. Octal escape
c. Hexadecimal
d. Line feed
Answer: A
7) What is the return type of the hashCode() method in the Object class?
a. Object
b. int
c. long
d. void
Answer: B
10) Evaluate the following Java expression, if x=3, y=5, and z=10:
++z + y - y + z + x++
a. 24
b. 23
c. 20
d. 25
Answer: A
2 powered by SPPUQuestionPapers.com
public class Test {
public static void main(String[] args) {
int count = 1;
while (count <= 15) {
System.out.println(count % 2 == 1 ? "***" : "+++++");
++count;
} // end while
} // end main
}
a. 15 times ***
b. 15 times +++++
c. 8 times *** and 7 times +++++
d. Both will print only once
Answer: C
12) Which of the following tool is used to generate API documentation in HTML format
from doc comments in source code?
a. javap tool
b. javaw command
c. Javadoc tool
d. javah command
Answer: C
13) Which of the following creates a List of 3 visible items and multiple selections abled?
a. new List(false, 3)
b. new List(3, true)
c. new List(true, 3)
d. new List(3, false)
Answer: B
15) Which method of the Class.class is used to determine the name of a class
represented by the class object as a String?
a. getClass()
b. intern()
c. getName()
d. toString()
Answer: C
16) In which process, a local variable has the same name as one of the instance
variables?
a. Serialization
b. Variable Shadowing
c. Abstraction
d. Multi-threading
3 powered by SPPUQuestionPapers.com
Answer: B
17) Which of the following is true about the anonymous inner class?
a. It has only methods
b. Objects can't be created
c. It has a fixed class name
d. It has no class name
Answer: D
22) switch(x)
{
default:
4 powered by SPPUQuestionPapers.com
System.out.println("Hello");
}
Which two are acceptable types for x?
1. byte
2.long
3.char
4.float
5.Short
6.Long
a. 1 and 3
b. 2 and 4
c. 3 and 5
d. 4 and 6
Answer: A
23) Which two statements are true about comparing two instances of the same class,
given that the equals() and hashCode() methods have been properly overridden?
1. If the equals() method returns true, the hashCode() comparison == must return true.
2. If the equals() method returns false, the hashCode() comparison != must return true.
3. If the hashCode() comparison == returns true, the equals() method must return true.
4. If the hashCode() comparison == returns true, the equals() method might return true.
a. 1 and 4
b. 2 and 3
c. 3 and 4
d. 1 and 3
Answer: A
26) Which interface provides the capability to store objects using a key-value pair?
5 powered by SPPUQuestionPapers.com
a. Java.util.Map
b. Java.util.Set
c. Java.util.List
d. Java.util.Collection
Answer: A
6 powered by SPPUQuestionPapers.com
{
MyThread t = new MyThread();
t.start();
System.out.print("one. ");
t.start();
System.out.print("two. ");
}
public void run()
{
System.out.print("Thread ");
}
}
a. Compilation fails
b. An exception occurs at runtime.
c. It prints "Thread one. Thread two."
d. The output cannot be determined.
Answer: B
7 powered by SPPUQuestionPapers.com
try
{
wait();
} catch(InterruptedException ex) { }
}
else
{
System.out.println("x is " + x++);
if (x >= 10)
{
notify();
}
}
}
}
a. The code will not compile because of an error on notify(); of class Foo.
b. The code will not compile because of some other error in class Test.
c. An exception occurs at runtime.
d. It prints "x is 5 x is 6".
Answer: C
switch (z)
8 powered by SPPUQuestionPapers.com
{
case 4: System.out.println("4 ");
case 5: System.out.println("5 ");
default: assert z < 10;
}
if ( z < 10 )
assert z > 4: z++; /* Line 22 */
System.out.println(z);
}
}
which line is an example of an inappropriate use of assertions?
a. Line 11
b. Line 12
c. Line 14
d. Line 22
Answer: D
9 powered by SPPUQuestionPapers.com
public static void main(String [] args)
{
boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */
System.out.print("ok ");
if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/
System.out.println("dokey");
}
}
a. dokey
b. ok dokey
c. No output is produced
d. Compilation error
Answer: A
10 powered by SPPUQuestionPapers.com
else
--hand;
System.out.println(hand);
}
}
a. 41
b. 42
c. 50
d. 51
Answer: D
11 powered by SPPUQuestionPapers.com
b. 0
c. Depends upon the type of variable
d. Not assigned
Answer: C
12 powered by SPPUQuestionPapers.com
a. Runtime polymorphism is a process in which a call to an overridden method is
resolved at runtime rather than at compile-time.
b. Runtime polymorphism is a process in which a call to an overloaded method is
resolved at runtime rather than at compile-time.
c. Both of the above.
d. None of the above.
Answer: A
13 powered by SPPUQuestionPapers.com
56) What is the size of boolean variable?
a. 8 bit
b. 16 bit
c. 32 bit
d. not precisely defined
Answer: B
58) Which of the following lines will compile without warning or error.
a. float f=1.3;
b. char c= 2.58;
c. byte b=257;
d. int i=10;
Answer: D
59) What will happen if you try to compile and run the following code ?
public class MyClass
{
public static void main(String arguments[])
{
amethod(arguments);
}
public void amethod(String[] arguments)
{
System.out.println(arguments);
System.out.println(arguments[1]);
}
}
a. error Can't make static reference to void amethod.
b. error method main not correct
c. error array must include parameter
d. amethod must be declared with String
Answer: A
61) What will be printed out if this code is run with the following command line?
java myprog good morning
14 powered by SPPUQuestionPapers.com
public class myprog{ public static void main(String argv[])
{
System.out.println(argv[2]);
}
}
a. myprog
b. good
c. morning
d. Exception raised: 'java.lang.ArrayIndexOutOfBoundsException: 2'
Answer: D
63) What will happen if you try to compile and run the following code?
public class Q
{
public static void main(String argv[])
{
int anar[]=new int[]{1,2,3};
System.out.println(anar[1]);
}
}
a. Error anar is referenced before it is initialized
b. Error: size of array must be defined
c. Compile and run without error
d. None of above
Answer: B
64) What will be printed out if you attempt to compile and run the following code?
int i=9;
switch (i)
{
default: System.out.println("default");
case 0: System.out.println("zero");
break;
case 1: System.out.println("one");
case 2: System.out.println("two");
}
a. default
b. default,zero
c. error default clause not defined
d. no output displayed
Answer: B
15 powered by SPPUQuestionPapers.com
65) What will happen if you attempt to compile and run the following code?
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx
{
public static void main(String argv[])
{
Base b=new Base();
Sub s=(Sub) b;
}
}
a. Compile and run without error
b. Compile time Exception
c. Runtime Exception
d. None of above
Answer: C
66) What will happen when you attempt to compile and run the following code?
public class Tux extends Thread
{
static String sName = "vandeleur";
public static void main(String argv[])
{
Tux t = new Tux();
t.piggy(sName);
System.out.println(sName);
}
public void piggy(String sName)
{
sName = sName + " wiggy";
start();
}
public void run()
{
for(int i=0;i < 4; i++)
{
sName = sName + " " + i;
}
}
}
a. Compile time error
b. Compilation and output of 'vandeleur wiggy'
c. Compilation and output of 'vandeleur wiggy 0 1 2 3'
d. Compilation and output of either 'vandeleur', 'vandeleur 0', 'vandeleur 0 1' 'vandaleur 0
1 2' or 'vandaleur 0 1 2 3'
Answer: D
16 powered by SPPUQuestionPapers.com
int i; int j;
outer: for (i=1;i <3;i++)
inner: for(j=1; j<3; j++)
{
if (j==2)
continue outer;
System.out.println("Value for i=" + i + " Value for j=" +j);
}
}
}
a. Value for i=1 Value for j=1
b. Value for i=3 Value for j=1
c. Value for i=2 Value for j=1
d. None of above
Answer: A
72) ________ is the mechanism that binds together the code and the data.
a. Polymorphism.
b. Encapsulation.
c. Inheritance.
d. Together.
Answer: B
17 powered by SPPUQuestionPapers.com
73) Java is designed for _______ environment of the internet.
a. Development
b. Deducting
c. Distributed
d. Web design
Answer: C
76) Once an interface has been defined, one or more _______ can implement that
interface.
a. Class
b. Object
c. Methods
d. Keywords
Answer: A
77) Variable declared as ________ do not occupy memory on a per instance basis.
a. Static
b. Final
c. Abstract
d. Code
Answer: B
78) _______ must be the first statement executed inside a subclass Constructor.
a. final()
b. super()
c. static{}
d. None of these
Answer: B
79) Which function is used to perform some action when the object is to be destroyed?
a. finalize()
b. delete()
c. main()
d. None of the above
Answer: A
18 powered by SPPUQuestionPapers.com
a. Break
b. Switch
c. Continue
d. Wait
Answer: A
84) Which exception is thrown by the read() method of Input Stream class?
a. Exception.
b. ClassNotFoundException.
c. ReadException.
d. IOException.
Answer: D
19 powered by SPPUQuestionPapers.com
c. Private
d. All of the above
Answer: A
89) ______ must be the first non comment statement in the file.
a. Package.
b. Class.
c. Object.
d. Declaration.
Answer: A
91) Which of the following is not a valid way to create String object?
a. String str2 = new String(new char[]{'a','b','c'});
b. String str = new String("abc");
c. String str1 = "abc";
d. String str3 = 'a'+'b'+'c';
Answer: D
20 powered by SPPUQuestionPapers.com
b. y James Ghosling
c. James Ghosling
d. Java was developed
Answer: A
95) Which type of exception will be thrown when you run this code ?
int a = 100, b = 0;
int c = a/b;
a. java.lang.Exception
b. java.lang.Throwable
c. java.lang.DivideByZeroException
d. java.lang.ArithmeticException
Answer: D
21 powered by SPPUQuestionPapers.com
99) What will be the Output of given code ?
public class Test implements Runnable
{
public void run()
{
System.out.println("r1 ");
System.out.println("r2 ");
}
100) Which class or interface defines the wait(), notify(), and notifyAll() methods ?
a. Object
b. Thread
c. Runnable
d. Class
Answer: A
22 powered by SPPUQuestionPapers.com