Copyright © 3S Technologies

Email: info@3s-technologies.com

Best viewed in Internet Explorer ver. 9.0 - 1024 X 768 resolution.

Java Objectives - Part 8

The reason for documenting this topic is to provide important technical questions and answers on Java language.  These questions can appear in an aptitude test, organized by different recruiting IT organizations.

 

Some questions and answers on AWT: CONTROLS, LAYOUT MANAGERS AND MENUS:

 

1) Which of the following statements are true?

a) The default layout manager for an Applet is FlowLayout

b) The default layout manager for an application is FlowLayout

c) A layout manager must be assigned to an Applet before the setSize method is called

d) The FlowLayout manager attempts to honor the preferred size of any components

Ans: a and d

 

2) Which method does display the messages whenever there is an item selection or deselection of the CheckboxMenuItem menu?

Ans: itemStateChanged method.

 

3) Which is a dual state menu item?

Ans: CheckboxMenuItem.

 

4) Which method can be used to enable/disable a checkbox menu item?

Ans: setState(boolean).

 

5) Which of the following may a menu contain?

a) A separator

b) A check box

c) A menu

d) A button

e) A panel

Ans: a and c

 

6) Which of the following may contain a menu bar?

a) A panel

b) A frame

c) An applet

d) A menu bar

e) A menu

Ans: b

 

7) What is the difference between a MenuItem and a CheckboxMenuItem?

Ans: The CheckboxMenuItem class extends the MenuItem class to support a menu item that may be checked or unchecked.

 

8) Which of the following are true?

a) A Dialog can have a MenuBar.

b) MenuItem extends Menu.

c) A MenuItem can be added to a Menu.

d) A Menu can be added to a Menu.

Ans: c and d.

 

 

9) Which colour is used to indicate instance methods in the standard "javadoc" format documentation:

1) blue

2) red

3) purple

4) orange

Ans: 2

 

NOTE: In JDK 1.1 the variables, methods and constructors are colour coded to simplify their identification.

 

10) What is the correct ordering for the import, class and package declarations when found in a single file?

1) package, import, class

2) class, import, package

3) import, package, class

4) package, class, import

Ans: 1

 

11) Which methods can be legally applied to a string object? (Choose 4)

1) equals(String)

2) equals(Object)

3) trim()

4) round()

5) toString()

Ans: 1,2,3,5

 

12) What is the parameter specification for the public static void main method? (Choose 2)

1) String args []

2) String [] args

3) Strings args []

4) String args

Ans: 1,2

 

13) What does the zeroth element of the string array passed to the public static void main method contain?

1) The name of the program

2) The number of arguments

3) The first argument if one is present

Ans: 3

 

14) Which of the following are Java keywords?

1) goto

2) malloc

3) extends

4) FALSE

Ans: 3

 

15) What will be the result of compiling the following code:

public class Test {

public static void main (String args []) {

int age;

age = age + 1;

System.out.println("The age is " + age);

}

}

 

1) Compiles and runs with no output

2) Compiles and runs printing out The age is 1

3) Compiles but generates a runtime error

4) Does not compile

5) Compiles but generates a compile time error

Ans: 4

 

16) Which of these is the correct format to use to create the literal char value a?

1) 'a'

2) "a"

3) new Character(a)

4) \000a

Ans: 1

 

17) What is the legal range of a byte integral type?

1) 0 - 65, 535

2) (-128) - 127

3) (-32,768) - 32,767

4) (-256) - 255

Ans: 2

 

18) Which of the following is illegal:

1) int i = 32;

2) float f = 45.0;

3) double d = 45.0;

Ans: 2

 

19) What will be the result of compiling the following code:

public class Test {

static int age;

public static void main (String args []) {

age = age + 1;

System.out.println("The age is " + age);

}

}

 

1) Compiles and runs with no output

2) Compiles and runs printing out The age is 1

3) Compiles but generates a runtime error

4) Does not compile

5) Compiles but generates a compile time error

Ans: 2

 

20) Which of the following are correct?

1) 128 >> 1 gives 64

2) 128 >>> 1 gives 64

3) 128 >> 1 gives -64

4) 128 >>> 1 gives -64

Ans: 1

 

21) Which of the following return true?

1) "john" == new String("john")

2) "john".equals("john")

3) "john" = "john"

4) "john".equals(new Button("john"))

Ans: 2

 

23) Which of the following do not lead to a runtime error?

1) "john" + " was " + " here"

2) "john" + 3

3) 3 + 5

4) 5 + 5.5

ans: 1,2,3,4

 

24) Which of the following are so called "short circuit" logical operators?

(multiple)

1) &

2) ||

3) &&

4) |

Ans: 2,3

 

25) Which of the following are acceptable? ( Choose two)

1) Object o = new Button("A");

2) Boolean flag = true;

3) Panel p = new Frame();

4) Frame f = new Panel();

5) Panel p = new Applet();

Ans: 1,5

 

26) What is the result of compiling and running the following code ?

public class Test {

static int total = 10;

public static void main (String args []) {

new Test();

}

public Test () {

System.out.println("In test");

System.out.println(this);

int temp = this.total;

if (temp > 5) {

System.out.println(temp);

}

}

}

 

1) The class will not compile

2) The compiler reports and error at line 2

3) The compiler reports an error at line 9

4) The value 10 is one of the elements printed to the standard output

5) The class compiles but generates a runtime error

Ans: 4

 

27) Which of the following is correct ?

1) String temp [] = new String {"j" "a" "z"};

2) String temp [] = { "j " " b" "c"};

3) String temp = {"a", "b", "c"};

4) String temp [] = {"a", "b", "c"};

Answer 4

 

28) What is the correct declaration of an abstract method that is intended to be public ?

1) public abstract void add();

2) public abstract void add() {}

3) public abstract add();

4) public virtual add();

Ans: 1

 

29) Under what situations do you obtain a default constructor?

1) When you define any class

2) When the class has no other constructors

3) When you define at least one constructor

Ans: 2

 

30) Which of the following can be used to define a constructor for this class, given the following code ?

public class Test {

...

}

1) public void Test() {...}

2) public Test() {...}

3) public static Test() {...}

4) public static void Test() {...}

Ans: 2

 

31) Which of the following are acceptable to the Java compiler ? (Choose four)

1) if (2 == 3) System.out.println("Hi");

2) if (2 = 3) System.out.println("Hi");

3) if (true) System.out.println("Hi");

4) if (2 != 3) System.out.println("Hi");

5) if (aString.equals("hello")) System.out.println("Hi");

Ans: 1,3,4,5

 

32) Assuming a method contains code which may raise an Exception (but not a RuntimeException), what is the correct way for a method to indicate that it expects the caller to handle that exception ?

1) throw Exception

2) throws Exception

3) new Exception

4) Don't need to specify anything

Ans: 2

 

33) What is the result of executing the following code, using the parameters 4 and 0 ?

public void divide(int a, int b) {

try {

int c = a / b;

} catch (Exception e) {

System.out.print("Exception ");

} finally {

System.out.println("Finally");

}

 

1) Prints out: Exception Finally

2) Prints out: Finally

3) Prints out: Exception

4) No output

Ans: 1

 

34) Which of the following is a legal return type of a method overloading the following method ?

public void add(int a) {...}

1) void

2) int

3) Can be anything

Ans: 3

 

35) Which of the following statements is correct for a method which is overriding the following method ?

public void add(int a) {...}

1) the overriding method must return void

2) the overriding method must return int

3) the overriding method can return whatever it likes

Ans: 1

 

36) Given the following classes defined in separate files, what will be the effect of compiling and running this class Test ?

class Vehicle {

public void drive() {

System.out.println("Vehicle: drive");

}

}

class Car extends Vehicle {

public void drive() {

System.out.println("Car: drive");

}

}

public class Test {

public static void main (String args []) {

Vehicle v;

Car c;

v = new Vehicle();

c = new Car();

v.drive();

c.drive();

v = c;

v.drive();

}

}

 

1) Generates a Compiler error on the statement v= c;

2) Generates runtime error on the statement v= c;

3) Prints out:

Vehicle: drive

Car: drive

Car: drive

4) Prints out:

Vehicle: drive

Car: drive

Vehicle: drive

Ans: 3

 

37) Where in a constructor, can you place a call to a constructor defined in the super class?

1) Anywhere

2) The first statement in the constructor

3) The last statement in the constructor

4) You can't call super in a constructor

Ans: 2

 

38) Which variables can an inner class access from the class which encapsulates it?

(multiple)

1) All static variables

2) All final variables

3) All instance variables

4) Only final instance variables

5) Only final static variables

Ans: 1,2,3

 

39) What class must an inner class extend ?

1) The top level class

2) The Object class

3) Any class or interface

4) It must extend an interface

Answer 3

 

 

3S Technologies

Online Education for all