Core Java Test Paper 2
Core Java Test Paper 2
Question 24)
Question 25)
following code?
Integer ten=new Integer(10);
Long nine=new Long (9);
System.out.println(ten + nine);
int i=1;
System.out.println(i + ten);
1) 19 followed by 20
2) 19 followed by 11
3) Error: Can't convert java lang Integer
4) 10 followed by 1
Question 33)
What will happen when you attempt to compile and run the
following code?.
1) It will compile and the run method will print out the
increasing value of i.
2) It will compile and calling start will print out the
increasing value of i.
3) The code will cause an error at compile time.
4) Compilation will cause an error because while cannot take a
parameter of true.
class Background implements Runnable{
int i=0;
public int run(){
while(true){
i++;
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
System.out.println("i="+i);
} //End while
return 1;
}//End run
}//End class
Question 38)
current directory.
Question 46)
You need to create a class that will store a unique object
elements. You do not need to sort these elements but they must
be unique.
What interface might be most suitable to meet this need?
1)Set
2)List
3)Map
4)Vector
Question 47)
Which of the following will successfully create an instance of
Question 49)
What will be the result when you attempt to compile this
program?
public class Rand{
public static void main(String argv[]){
int iRand;
iRand = Math.random();
System.out.println(iRand);
}
}
1) Compile time error referring to a cast problem
2) A random number between 1 and 10
3) A random number between 0 and 1
4) A compile time error about random being an unrecognised
method
Question 50)
Given the following code
import java.io.*;
public class Th{
public static void main(String argv[]){
Th t = new Th();
t.amethod();
}
public void amethod(){
try{
ioCall();
}catch(IOException ioe){}
}
}
What code would be most likely for the body of the ioCall
method
1) public void ioCall ()throws IOException{
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}
2) public void ioCall ()throw IOException{
DataInputStream din = new DataInputStream(System.in);
din.readChar();
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
}
3) public void ioCall (){
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}
4) public void ioCall throws IOException(){
DataInputStream din = new DataInputStream(System.in);
din.readChar();
}
Question 53)
Which of the following can you perform using the File class?
1) Change the current directory
2) Return the name of the parent directory
3) Delete a file
4) Find if a file contains text or binary information
Question 55)
You are concerned that your program may attempt to use more
ensure that the Java Virtual Machine will run its garbage
want .
1) You cannot be certain when garbage collection will run
2) Use the Runtime.gc() method to force garbage collection
3) Ensure that all the variables you require to be garbage
jd2
8) What will happen when you attempt to compile and run this
code?
private class Base{}
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
public class Vis{
transient int iVal;
public static void main(String elephant[]){
}
}
1)Compile time error: Base cannot be private
2)Compile time error indicating that an integer cannot be
transient
3)Compile time error transient not a data type
4)Compile time error malformed main method
9) What happens when you attempt to compile and run these two
Question 18)
What will happen when you attempt to compile and run the
following code?
public class Bground extends Thread{
Question 20)
Which most closely matches a description of a Java Map?
1) A vector of arrays for a 2D geographic representation
2) A class for containing unique array elements
3) A class for containing unique vector elements
4) An interface that ensures that implementing classes cannot
Question 21)
How does the set collection deal with duplicate elements?
1) An exception is thrown if you attempt to add an element
Question 22)
What can cause a thread to stop executing?
1) The program exits via a call to System.exit(0);
2) Another thread is given a higher priority
3) A call to the thread's stop method.
4) A call to the halt method of the Thread class
Question 23)
For a class defined inside a method, what rule governs access
Question 24)
Under what circumstances might you use the yield method of the
Thread class
1) To call from the currently running thread to allow another
Question 25)
What will happen when you attempt to compile and run the
following code
public class Hope{
public static void main(String argv[]){
Hope h = new Hope();
}
protected Hope(){
for(int i =0; i <10; i ++){
System.out.println(i);
}
}
}
1) Compilation error: Constructors cannot be declared
protected
2) Run time error: Constructors cannot be declared protected
3) Compilation and running with output 0 to 10
4) Compilation and running with output 0 to 9
Question 26)
What will happen when you attempt to compile and run the
following code
public class MySwitch{
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
int k=10;
switch(k){
default: //Put the default at the bottom, not here
System.out.println("This is the default output");
break;
case 10:
System.out.println("ten");
case 20:
System.out.println("twenty");
break;
}
}
}
1) None of these options
2) Compile time errror target of switch must be an integral
type
3) Compile and run with output "This is the default output"
4) Compile and run with output "ten"
Question 27)
Which of the following is the correct syntax for suggesting
Question 28)
What will happen when you attempt to compile and run the
following code
public class As{
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
int i = 10;
int j;
char z= 1;
boolean b;
public static void main(String argv[]){
As a = new As();
a.amethod();
}
public void amethod(){
System.out.println(j);
System.out.println(b);
}
}
1) Compilation succeeds and at run time an output of 0 and
false
2) Compilation succeeds and at run time an output of 0 and
true
3) Compile time error b is not initialised
4) Compile time error z must be assigned a char value
Question 29)
What will happen when you attempt to compile and run the
Question 30)
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
What will happen when you attempt to compile and run the
following code
public class StrEq{
==
Question 31)
1) What will happen when you attempt to compile and run the
following code
import java.io.*;
class Base{
public static void amethod()throws FileNotFoundException{}
}
protected ExcepDemo(){
try{
DataInputStream din = new DataInputStream(System.in);
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
System.out.println("Pausing");
din.readChar();
System.out.println("Continuing");
this.amethod();
}catch(IOException ioe) {}
}
}
1)Compile time error caused by protected constructor
2) Compile time error caused by amethod not declaring
Exception
3) Runtime error caused by amethod not declaring Exception
4) Compile and run with output of "Pausing" and "Continuing"
Question 32)
What will happen when you attempt to compile and run this
program
public class Outer{
public String name = "Outer";
public static void main(String argv[]){
Inner i = new Inner();
i.showName();
}//End of main
}
1) Compile and run with output of "Outer"
2) Compile and run with output of "Inner"
3) Compile time error because Inner is declared as private
4) Compile time error because of the line creating the
instance of Inner
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
Question 34)
Which option most fully describes will happen when you attempt
Question 35)
Which of the following will compile correctly
1) short myshort = 99S;
2) String name = 'Excellent tutorial Mr Green';
3) char c = 17c;
4)int z = 015;
Question 36)
Which of the following are Java key words
1)double
2)Switch
3)then
4)instanceof
Question 37
What will be output by the following line?
System.out.println(Math.floor(-2.1));
1) -2
2) 2.0
3) -3
4) -3.0
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
Question 38)
Given the following main method in a class called Cycle and a
command line of
java Cycle one two
what will be output?
public static void main(String bicycle[]){
System.out.println(bicycle[0]);
}
1) None of these options
2) cycle
3) one
4) two
Question 39)
Which of the following statements are true?
1) At the root of the collection hierarchy is a class called
Collection
2) The collection interface contains a method called
enumerator
3) The interator method returns an instance of the Vector
class
4) The set interface is designed for unique elements
Question 41)
Given the following code
class Base{}
1) b=m;
2) m=b;
3) d =i;
4) b1 =i;
Question 42)
Which of the following statements about threading are true
1) You can only obtain a mutually exclusive lock on methods in
Question 43)
Your chief Software designer has shown you a sketch of the new
runs the Linux operating System and the other runs the Windows
WindowsPC are two Sub classes one called Server and one Called
soon be obsolete
4) Change the hierarchy to remove the need for the superfluous
Computer Class.
Question 44)
Objective 4.1)
Which of the following statements are true
1) An inner class may be defined as static
2) There are NO circumstances where an inner class may be
defined as private
3) An anonymous class may have only one constructor
4) An inner class may extend another class
Question 45)
What will happen when you attempt to compile and run the
following code
int Output=10;
boolean b1 = false;
if((b1==true) && ((Output+=10)==20)){
System.out.println("We are equal "+Output);
}else
{
System.out.println("Not equal! "+Output);
}
1) Compile error, attempting to peform binary comparison on
Question 49)
Which of the following will compile without error?
1) File f = new File("/","autoexec.bat");
2) DataInputStream d = new DataInputStream(System.in);
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
3) OutputStreamWriter o = new OutputStreamWriter(System.out);
4) RandomAccessFile r = new RandomAccessFile("OutFile");
Question 50)
Given the folowing classes which of the following will compile
without error?
interface IFace{}
class CFace implements IFace{}
class Base{}
Question 51)
Given the following code what will be the output?
class ValHold{
public int i = 10;
}
public class ObParm{
public static void main(String argv[]){
ObParm o = new ObParm();
o.amethod();
}
public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
}//End of amethod
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
public void another(ValHold v, int i){
i=0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
1) 10,0, 30
2) 20,0,30
3) 20,99,30
4) 10,0,20
Question 52)
Given the following class definition, which of the following
Question 53)
Given the following class definition which of the following
}
}
1)MyOver m = new MyOver();
2)super();
3)this("Hello",10);
4)Base b = new Base(10);
Question 54)
Given the following class definition, which of the following
Question 55)
Which of the following are methods of the Thread class?
1) yield()
2) sleep(long msec)
3) go()
4) stop()
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
Question 56)
Which of the following methods are members of the Vector class
Question 57)
Which of the following statements are true?
1) Adding more classes via import statements will cause a
private modifier
3) A inner class may under some circumstances be defined with
Question 59)
Which of the following are methods of the Collection
interface?
1) iterator
2) isEmpty
3) toArray
4) setText
Question 60)
Which of the following best describes the use of the
synhronized keyword?
1) Allows two process to run in paralell but to communicate
object
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
3) Ensures that two or more processes will start and end at
same time
JD4
Q9. What is the output of following if the return value is "the value 0 if the argument
string is equal to this string; a value less than 0 if this string is lexicographically less than
the string argument; and a value greater than 0 if this string is lexicographically greater
than the string argument" (Assuming written inside main)
String s5 = "AMIT";
String s6 = "amit";
System.out.println(s5.compareTo(s6));
System.out.println(s6.compareTo(s5));
System.out.println(s6.compareTo(s6));
Ans
a> -32
32
0
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
b> 32
32
0
c> 32
-32
0
d> 0
0
0
Q27)
1 Boolean b1 = new Boolean("TRUE");
2 Boolean b2 = new Boolean("true");
3 Boolean b3 = new Boolean("JUNK");
4 System.out.println("" + b1 + b2 + b3);
a) Comiler error
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
b) RunTime error
c)truetruefalse
d)truetruetrue
Q39) Which one of the following methods of java.io.File throws IOException and why
a) getCanonicalPath and getAbsolutePath both require filesystem queries.
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
b) Only getCannonicalPath as it require filesystem queries.
c) Only getAbsolutePath as it require filesystem queries.
c) 12345.msg
null
false
12345.msg
true
true
d) \12345.msg
\
true
12345.msg
false
false
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
Question 1
What will happen if you compile/run this code?
Question 2
What will happen if you compile/run the following code?
1: class Test
2: {
3: static void show()
4: {
5: System.out.println("Show method in Test class");
6: }
7:}
8:
9: public class Q2 extends Test
10: {
11: static void show()
12: {
13: System.out.println("Show method in Q2 class");
14: }
15: public static void main(String[] args)
16: {
17: Test t = new Test();
Haaris Infotech - Shoiab - 9840135749
Core Java - Test - 50 Marks
18: t.show();
19: Q2 q = new Q2();
20: q.show();
21:
22: t = q;
23: t.show();
24:
25: q = t;
26: q.show();
27: }
28: }
D) Compilation error.