Test Corejava
1) Which of the following is a correct signature for the main method? [0.5]
a) static void main(String[] args[]) c) public void main(String[] args)
*b) public static void main(String[] args) d) public static void main(Strings[] args)
2) What value does readLine() return when it has reached the end of the file? [0.5]
a) 1 c) 0
b) -1 *d) null
3) Which of the Boolean expressions below is incorrect? (choose 2) [0.5]
*a) (true) && (3 => 4) c) (x > 0) || (x < 0)
b) !(x > 0) && (x > 0) *d) (x != 0) || (x = 0)
4) To declare an int variable number with initial value 2, you write [0.5]
a) int number = 2L; *c) int number = 2;
b) int number = 2l; d) Int number = 2.0;
5) What value does read () return when it has reached the end of the file? [0.5]
a) 1 c) 0
*b) -1 d) Null
6) Which of these data type requires the largest amount of memory? [0.5]
a) Float c) short
*b)
Double
d) byte
7) Suppose x=0 and y=0 what is x after evaluating the expression (y >=0) || (x++ > 0)? [1]
a) 1 *c) 0
b) -1 d) null
8) Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement?
if (x > 0)
if (y > 0)
System.out.println(“x > 0 and y > 0”);
else if (z > 0)
System.out.println(“x < 0 and z > 0”);
[0.5]
a) x > 0 and y > 0; c) x<0 and z < 0;
*b) x < 0 and z > 0; d) None of the above.
2002 / Sem 1 – ACCP 2003 Q.P. Page 1 of 7
9) What is y after the following switch statement is executed?
x = 3;
switch (x+3)
{
case 6: y = 0;
case 7: y = 1;
default: y += 2;
}
[0.5]
a) 1 c) 0
b) 2 *d) 3
10) What is sum after the following loop terminates?
int sum = 0;
int item = 0;
do
{
item++;
sum += item;
if (sum > 7) break;
}
while (item < 6);
[1]
*a) 10 c) 6
b) 5 d) 8
11) Analyze the following code:
public class Test
{
public static void main (String args[])
{
int i = 0;
for (i=0; i<11; i++);
System.out.println(i+4);
}
}
[0.5]
a) The program has a syntax error
because of the semicolon (;) on
the for loop line.
c) The program compiles despite the semicolon (;)
on the for loop line, and displays 14.
*b) The program compiles despite
the semicolon (;) on the for loop
line, and displays 15.
d) None of the above.
12) What is wrong with the following if statement?(choose 2)
if(i++) {
y = x * z;
x /= 2;
else {
[1]
y = y * y;
++z;
}
a) The if condition should use a prefix
operator instead of a postfix operator.
*c) There is a missing } before the else.
*b) The if condition must be a boolean
expression, not a numeric expression.
d) There is no break statement to allow control
to transfer out of the if statement.
13) Which of the following modifiers must be present for a method inside the main class
to be called inside the main method?
[1]
a) public *c) static
b)
private
d) None of the above is necessary.
14) Which of the following are illegal identifiers? (choose 2) [1]
*a) 2variable c) _whatavariable
b) variable2 d) _3_
e) $anothervar *f) #myvar
15) What are the three ways in which a thread can enter the waiting state?(choose 3) [0.5]
*a) Sleep() *c) Suspend()
*b) Wait() d) Start()
e)
Stop()
16) Which of the following are true about the finally clause of a try-catch-finally
statement?
[0.5]
a) It is only executed after a catch clause has executed
b) It is only executed after a catch clause has not executed
*c) It is always executed unless its thread terminates
d) It is only executed if an exception is thrown
17) Which layout manager can divide the container into a grid ? [0.5]
a) CardLayout c) BorderLayout
b) FlowLayout *d) GridLayout
18)
Which of the following will output -4.0 [1]
a) System.out.println(Math.floor(-4.7)); *c)
System.out.println(Math.ceil(-4.7));
b) System.out.println(Math.round(-4.7)); d) System.out.println(Math.min(-4.7));
19) Which of the following is the super class of these classes - ContainterEvent,
FocusEvent, InputEvent, PaintEvent, WindowEvent. Select the one correct answer.
[1]
a) ActionEvent
b) AdjustmentEvent
*c) ComponentEvent
d) ItemEvent
e) TextEvent
f) Event
20) _________ and ________ are required attributes that give the size ( in pixels) of the
applet display area.
[0.5]
*a) width, height
b) vspace, hspace
c) param name, param value
21) Which methods will cause a Frame to be displayed? [0.5]
*a) show() c) Display()
b) SetVisible() d) DisplayFrame()
22) Which of these is a legal definition of a method named m assuming it throws
IOException, and returns void. Also assume that the method does not take any
arguments. Select all correct answers.
[0.5]
*a) void m() throws IOException{} c) void m(void) throws IOException{}
b) void m() throw IOException{} d) m() throws IOException{}
23) The ___________ class is the superclass of the Exception class [0.5]
a) Exception c) Error
*b) Throwable d) Runtime Exception
24)
static void nPrint(String message, int n)
{
while (n > 0)
{
System.out.print(message);
n--;
}
}
What is k after invoking nPrint()?
int k = 2;
nPrint(“A message”, k);
[0.5]
a) 0
b) 1
*c) 2
d)
None of the above.
25)
What will be printed out if this code is run with the following command line?
Java myprog good morning sir
public class myprog{
Public static void main(String argv[])
{
System.out.println(argv[0])
}
[0.5]
}
a) myprog
*b) good
c) morning
d) Exception raised: “java.lang.ArrayIndexOutOfBoundsException: 2”
26)
What most closely matches the appearance when this code runs?
Import java.awt.*;
Public class CompLay extends Frame{
Public static void main(String argv[]){
CompLay cl = new CompLay();
}
CompLay(){
Panel p = new Panel();
p.setBackground(Color.pink);
p.add(new Button(“One”));
p.add(new Button(“Two”));
p.add(new Button(“Three”));
add(“South”,p);
setLayout(new FlowLayout());
setSize(300,300);
setVisible(true);
}
}
[1]
a) The buttons will run from left to right along the bottom of the Frame
*b) The buttons will run from left to right along the top of the frame
c) The buttons will not be displayed
d) Only button three will show occupying all of the frame
27) Which is the correct syntax for to draw a line using the drawLine() method? [0.5]
*a.
void drawLine(int startX, int startY, int endX, int endY) c) void drawLine(int startX, int endX)
b void drawLine(int startX, int startY, int startZ)
28)
Suppose that f is an instance of Foo
class Foo
{
int i;
static int s;
void imethod()
{ }
static void smethod()
{ }
}
Which of the following statements is
incorrect?
[1]
A. System.out.println(Foo.s);
*B.System.out.println(Foo.i);
C. f.imethod();
D. Foo.smethod();