Beruflich Dokumente
Kultur Dokumente
Question 1.
Consider the following array definitions:
int[] array1, array2[];
int[][] array3;
int[] array4[], array5[];
Which of the follwing are valid statements?
b array2 = array4;
c array1 = array2;
d array4 = array1;
e array5 = array3
Question 2.
Which statements concerning the value of a member variable are true, when it is not initialized explicitly?
Question 3.
Given the following class, which statements can be inserted at line 1 without causing the code to fail
compilation?
b b++;
c c++;
d d++;
e e++;
Question 4.
Consider following two classes:
What will be the output of compiling and running class B ?
Question 5.
Which line contains a valid constructor in the following class definition?
public class TestClass
{
int i, j;
public TestClass getInstance() { return new TestClass(); } //1
public void TestClass(int x, int y) { i = x; j = y; } //2
public TestClass TestClass() { return new TestClass(); } //3
public ~TestClass() { i = x; j = y; } //4
}
b Line 2
c Line 3
d Line 4
Question 6
Which lines contain a valid constructor in the following code?
b Line // 2
c Line // 3
d Line // 4
e Line // 5
Question 7
Compared to public, protected and private accessibility, default accessibility is....
Question 8
How can you declare 'i' so that it is not visible outside the package 'test'.
package test;
class Test
{
XXX int i;
/* lot of code */
}
b public
It will make it available to everybody.
c protected
It will make it available to a subclass even if the subclass is in a different package.
d No access modifier
e friend
There is no such modifier in Java.
Question 9
Consider the following program...
class ArrayTest
{
public static void main(String[] args)
{
int ia[][] = { {1, 2}, null };
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
System.out.println(ia[i][j]);
}
}
Question 11
Consider the following classes...
(See Exhibit)
Which of the following method declarations would be valid at line //1 ?
class Teacher
{
void teach(String student)
{
/* lots of code */
}
}
class Prof extends Teacher
{
//1
}
Question 12
What is the correct declaration of an abstract public method 'add' accessible to everybody, takes no
arguments and returns nothing?
(Use only one space between words)
Correct Answer(s)
a. public abstract void add();
Question 13
Which of the following statements is correct?
Question 14
Consider the following class...
What will happen when you attempt to compile and run the program?
import java.awt.*;
import java.awt.event.*;
class TestFrame extends Frame
{
String s="Message";
public static void main(String args[])
{
TestFrame t = new TestFrame();
Button b = new Button("press me");
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
System.out.println("Message is " +s);
}
}
);
t.add(b);
}
}
d It will compile and show a frame big enough to display the button.
e None of the above.
Question 15
The following is a valid member variable declaration ...
b False
Question 16
Which statement regarding the following code is true?
int a, b;
b = 5;
Question 17
Which one of these is a proper definition of a class TestClass that cannot be sub-classed?
Question 18
What will the following program print ?(See Exhibit)
class Test
{
public static void main(String[] args)
{
int i = 4;
int ia[][][] = new int[i][i = 3][i];
System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length);
}
}
b 3, 4, 3
c 3, 3, 3
d 4, 3, 4
e 4, 3, 3
Question 19
Which of these are not legal declarations within a class?
c abstract int t ;
Question 20
Which of the following are valid declarations of the standard main() method?
Question 21
What, if anything, is wrong with the following code?
Question 22
Which line, if any, will give a compile time error ?
void test(byte x)
{
switch(x)
{
case 'a': // 1
case 256: // 2
case 0: // 3
default : // 4
case 80: // 5
}
}
d Line 3 as the default label must be the last label in the switch statement.
Question 23
What is the correct parameter specification for the standard main method?
b String[ ] args
c Strings args[ ]
d String args
e String args[ ]
Question 24
Which of these combinations of switch expression types and case label value types are legal within a switch
statement?
b switch expression of type float and case label value of type int.
c switch expression of type byte and case label value of type float.
d switch expression of type char and case label value of type byte.
e switch expression of type boolean and case label value of type boolean.
Question 25
Consider:
Which of the following statements are valid at line 1 (Select the best answer).
b System.out.println(i);
c System.out.println(Outer.this.i);
Question 26
Which of these statements are true?
Question 27
Given the following class, which of these are valid ways of referring to the class from outside of the
package com.enthu?
package com.enthu;
public class Base
{
// ....
// lot of code...
}
Question 28
Which of these statements are true?
b A class may contain both static and non-static variables and both static and non-static methods. *
c Each object of a class has its own instance of each non-static member variable. *
e All methods in a class are implicitly passed a 'this' parameter when called.
Question 29
Which of the following is not a legal Java identifier?
b int123
c 2Next
d _interface
e a$_123
Question 30
For object o1 of class A to access a member(field or method) of object o2 of class B, when the member has
no access modifier, class B must be...
c a Super class of A
Question 31
Given the following classes and declarations, which of these statements about //1 and //2 are true?
(See Exhibit)
class A
{
private int i = 10;
public void f(){}
public void g(){}
}
class B extends A
{
public int i = 20;
public void g(){}
}
public class C
{
A a = new A();//1
A b = new B();//2
}
Question 32
Which of the following code fragments are valid method declarations?
Select 1 correct option.
a void method1{ }
b void method2( ) { }
c void method3(void){ }
d method4{ }
e method5(void){ }
Question 33
Which of the following code fragments will successfully initialize a two-dimensional array of chars named
cA with a size such that cA[2][3] refers to a valid element?
3. char cA[][] = { new char[ ]{ 'a', 'b', 'c' } , new char[ ]{ 'a', 'b', 'c' } };
4. char cA[3][2] = new char[][] { { 'a', 'b', 'c' }, { 'a', 'b', 'c' } };
b 4, 5
c 2, 3
d 1, 2, 3
e 2
Question 34
Which of the following correctly declare a variable which can hold an array of 10 integers?
b int[10] iA
c int iA[ ]
d Object[ ] iA
e Object[10] iA
Question 35
Given the following code, which statements can be placed at the indicated position without causing
compile and run time errors?
public class Test
{
int i1;
static int i2;
public void method1()
{
int i;
// ... insert statements here
}
}
b i = this.i2;
d this.i = 4;
e this.i1 = i2;