Wednesday, January 21, 2009

Java Interview Questions

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

1) blue 2) red 3) purple 4) orange

Answer : 2

explain

In JDK 1.1 the variabels, methods and constructors are colour coded to simplifytheir identification.

endExplain

2. 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

Answer : 1

explain

This is my explanation for question 2

endExplain

3. Which methods can be legally applied to a string object?

(Multiple)

1) equals(String) 2) equals(Object) 3) trim() 4) round() 5) toString()

Answer : 1,2,3,5

4. What is the parameter specification for the public static void main method?

(multiple)

1) String args [] 2) String [] args 3) Strings args [] 4) String args

Answer : 1,2

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

(multiple)

1) The name of the program 2) The number of arguments

3) The first argument if one is present

Answer : 3

6. Which of the following are Java keywords?

(multiple)

1) goto 2) malloc 3) extends 4) FALSE

Answer : 3

7. 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

Answer : 4

8. Which of these is the correct format to use to create the literal char value a?

(multiple)

1) 'a' 2) "a" 3) new Character(a) 4) \000a

Answer : 1

9. What is the legal range of a byte integral type?

1) 0 - 65, 535 2) (-128) – 127 3) (-32,768) - 32,767

Answer : 2

10. Which of the following is illegal:

1) int i = 32; 2) float f = 45.0; 3) double d = 45.0;

Answer 2

11. 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

Answer : 2

12. Which of the following are correct?

(multiple)

1) 128 >> 1 gives 64 2) 128 >>> 1 gives 64

Answer : 1

13. Which of the following return true?

(multiple)

1) "john" == new String ("john") 2) "john".equals("john") 3) "john" = "john"

Answer : 2

14. Which of the following do not lead to a runtime error?

(multiple)

1) "john" + " was " + " here" 2) "john" + 3 3) 3 + 5 4) 5 + 5.5

answer 1,2,3,4

15. Which of the following are so called "short circuit" logical operators?

(multiple)

1) & 2) || 3) && 4) |

Answer : 2,3

16. Which of the following are acceptable?

(multiple)

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();

Answer : 1,5

17. 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);

}

}

}

(multiple)

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

Answer : 4

18. 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

19. 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() {}

Answer : 1

20. 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

Answer : 2

21. 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() {...}

Answer : 2

22. Which of the following are acceptable to the Java compiler:

(multiple)

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");

Answer : 1,3,4,5

23. 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

Answer : 2

24. 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

Answer : 1

25. 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

Answer : 3

26. 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

Answer : 1

27. 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

Answer : 3

28. 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

Answer : 2

29. 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

Answer : 1,2,3

30. What class must an inner class extend:

1) The top level class

2) The Object class

3) Any class or interface

Answer 3

31. In the following code, which is the earliest statement, where the object originally held in e, may be garbage collected:

1. public class Test {

2. public static void main (String args []) {

3. Employee e = new Employee("Bob", 48);

4. e.calculatePay();

5. System.out.println(e.printDetails());

6. e = null;

7. e = new Employee("Denise", 36);

8. e.calculatePay();

9. System.out.println(e.printDetails());

10. }

11. }

1) Line 10

2) Line 11

3) Line 7

Answer : 3

32. What is the name of the interface that can be used to define a class that can execute within its own thread?

1) Runnable 2) Run 3) Threadable 4) Thread 5) Executable

Answer : 1

33. What is the name of the method used to schedule a thread for execution?

1) init(); 2) start(); 3) run(); 4) resume(); 5) sleep();

Answer : 2

34. Which methods may cause a thread to stop executing?

(multiple)

1) sleep(); 2) stop(); 3) yield(); 4) wait(); 5) notify(); 6) notifyAll()

7) synchronized()

Answer : 1,2,3,4

35. Which of the following would create a text field able to display 10 characters (assuming a fixed size font) displaying the initial string "hello":

1) new TextField("hello", 10); 2) new TextField("hello"); 3) new textField(10);

Answer : 1

36. Which of the following methods are defined on the Graphics class:

(multiple)

1) drawLine(int, int, int, int)

2) drawImage(Image, int, int, ImageObserver)

3) drawString(String, int, int)

4) add(Component); 5) setVisible(boolean); 6) setLayout(Object);

Answer : 1,2,3

37. Which of the following layout managers honours the preferred size of a component:

(multiple)

1) CardLayout 2) FlowLayout 3) BorderLayout 4) GridLayout

Answer : 2

38. Given the following code what is the effect of a being 5:

public class Test {

public void add(int a) {

loop: for (int i = 1; i <>

for (int j = 1; j <>

if (a == 5) {

break loop;

}

System.out.println(i * j);

} } }

}

1) Generate a runtime error

2) Throw an ArrayIndexOutOfBoundsException

3) Print the values: 1, 2, 2, 4

4) Produces no output

Answer : 4

39. What is the effect of issuing a wait() method on an object

1) If a notify() method has already been sent to that object then it has no effect

2) The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method

3) An exception will be raised

4) The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.

Answer : 2

40. The layout of a container can be altered using which of the following methods:

(multiple)

1) setLayout(aLayoutManager);

2) addLayout(aLayoutManager); 3) layout(aLayoutManager);

Answer : 1

41. Using a FlowLayout manager, which is the correct way to add elements to a container:

1) add(component); 2) add("Center", component); 3) add(x, y, component);

Answer : 1

42. Given that a Button can generate an ActionEvent which listener would you expect to have to implement, in a class which would handle this event?

1) FocusListener 2) ComponentListener 3) WindowListener

4) ActionListener 5) ItemListener

Answer : 4

43. Which of the following, are valid return types, for listener methods:

1) boolean 2) the type of event handled 3) void 4) Component

Answer : 3

44. Assuming we have a class which implements the ActionListener interface, which method should be used to register this with a Button?

1) addListener(*); 2) addActionListener(*); 3) addButtonListener(*);

Answer : 2

45. In order to cause the paint(Graphics) method to execute, which of the following is the most appropriate method to call:

1) paint() 2) repaint() 3) paint(Graphics) 4) update(Graphics)

Answer : 2

46. Which of the following illustrates the correct way to pass a parameter into an applet:

1)

2)

3)

Answer : 2

47. Which of the following correctly illustrate how an InputStreamReader can be created:

(multiple)

1) new InputStreamReader(new FileInputStream("data"));

2) new InputStreamReader(new FileReader("data"));

3) new InputStreamReader(new BufferedReader("data"));

4) new InputStreamReader("data");

5) new InputStreamReader(System.in);

Answer : 1,5

48. What is the permanent effect on the file system of writing data to a new FileWriter("report"), given the file report already exists?

1) The data is appended to the file

2) The file is replaced with a new file

Answer : 2

49. What is the effect of adding the sixth element to a vector created in the following manner:

new Vector(5, 10);

1) An IndexOutOfBounds exception is raised.

2) The vector grows in size to a capacity of 10 elements

3) The vector grows in size to a capacity of 15 elements

4) Nothing, the vector will have grown when the fifth element was added

Answer : 3

50. What is the result of executing the following code when the value of x is 2:

switch (x) {

case 1:

System.out.println(1);

case 2:

case 3:

System.out.println(3);

case 4:

System.out.println(4);

}

1) Nothing is printed out 2) The value 3 is printed out

3) The values 3 and 4 are printed out 4) The values 1, 3 and 4 are printed out

Answer : 3

51. What is the result of compiling and running the Second class?

Consider the following example:

class First {

public First (String s) {

System.out.println(s);

}

}

public class Second extends First {

public static void main(String args []) {

new Second();

}

}

3) An instance of the class First is generated 4) An instance of the class Second is created

5) An exception is raised at runtime stating that there is no null parameter constructor in class First.

6) The class second will not compile as there is no null parameter constructor in the class First

Answer : 6

52. What is the result of executing the following fragment of code:

boolean flag = false;

if (flag = true) {

System.out.println("true");

} else {

System.out.println("false");

}

1) true is printed to standard out 2) false is printed to standard out

3) An exception is raised

4) Nothing happens

Answer : 1

53. Consider the following classes. What is the result of compiling and running this class?

public class Test {

public static void test() {

this.print();

}

public static void print() {

System.out.println("Test");

}

public static void main(String args []) {

test();

}

}

(multiple)

1) The string Test is printed to the standard out.

2) A runtime exception is raised stating that an object has not been created.

3) Nothing is printed to the standard output.

4) An exception is raised stating that the method test cannot be found.

5) An exception is raised stating that the variable this can only be used within an instance.

6) The class fails to compile stating that the variable this is undefined.

Answer : 6

54. Examine the following class definition:

public class Test {

public static void test() {

print();

}

public static void print() {

System.out.println("Test");

}

public void print() {

System.out.println("Another Test");

}

}

What is the result of compiling this class:

1) A successful compilation.

2) A warning stating that the class has no main method.

3) An error stating that there is a duplicated method.

4) An error stating that the method test() will call one or other of the print() methods.

Answer : 3

55. What is the result of compiling and executing the following Java class:

public class ThreadTest extends Thread {

public void run() {

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

suspend();

resume();

System.out.println("Leaving run");

}

public static void main(String args []) {

(new ThreadTest()).start();

}

}

1) Compilation will fail in the method main.

2) Compilation will fail in the method run.

3) A warning will be generated for method run.

4) The string "In run" will be printed to standard out.

Answer : 4

56. Given the following sequence of Java statements, Which of the following options are true:

1. StringBuffer sb = new StringBuffer("abc");

2. String s = new String("abc");

3. sb.append("def");

4. s.append("def");

5. sb.insert(1, "zzz");

6. s.concat(sb);

7. s.trim();

(multiple)

4) The compiler would generate an error for line 4.

5) The compiler would generate an error for line 5.

6) The compiler would generate an error for line 6.

Answer : 4,6

57. What is the result of executing the following Java class:

import java.awt.*;

public class FrameTest extends Frame {

public FrameTest() {

add (new Button("First"));

add (new Button("Second"));

add (new Button("Third"));

pack();

setVisible(true);

}

public static void main(String args []) {

new FrameTest();

}

}

1) Nothing happens.

5) Only the "second" button is displayed.

6) Only the "third" button is displayed.

Answer : 6

58. Consider the following tags and attributes of tags, which can be used with the and

tags?

1. CODEBASE

2. ALT

3. NAME

4. CLASS

5. JAVAC

6. HORIZONTALSPACE

7. VERTICALSPACE

8. WIDTH

9. PARAM

10. JAR

(multiple)

1) line 1, 2, 3

2) line 2, 5, 6, 7

3) line 3, 4, 5

4) line 8, 9, 10

5) line 8, 9

Answer : 1,5

59. Which of the following is a legal way to construct a RandomAccessFile:

1) RandomAccessFile("data", "r");

2) RandomAccessFile("r", "data");

3) RandomAccessFile("data", "read");

4) RandomAccessFile("read", "data");

Answer : 1

60. Carefully examine the following code, When will the string "Hi there" be printed?

public class StaticTest {

static {

System.out.println("Hi there");

}

public void print() {

System.out.println("Hello");

}

public static void main(String args []) {

StaticTest st1 = new StaticTest();

st1.print();

StaticTest st2 = new StaticTest();

st2.print();

}

}

1) Never.

2) Each time a new instance is created.

3) Once when the class is first loaded into the Java virtual machine.

4) Only when the static method is called explicitly.

Answer : 3

61. What is the result of the following program:

public class Test {

public static void main (String args []) {

boolean a = false;

if (a = true)

System.out.println("Hello");

else

System.out.println("Goodbye");

}

}

1) Program produces no output but terminates correctly. 2) Program does not terminate.

3) Prints out "Hello" 4) Prints out "Goodbye"

Answer : 3

62. Examine the following code, it includes an inner class, what is the result:

public final class Test4 {

class Inner {

void test() {

if (Test4.this.flag); {

sample();

} } }

private boolean flag = true;

public void sample() {

System.out.println("Sample");

}

public Test4() {

(new Inner()).test();

}

public static void main(String args []) {

new Test4();

} }

1) Prints out "Sample"

2) Program produces no output but terminates correctly.

3) Program does not terminate.

4) The program will not compile

Answer : 1

63. Carefully examine the following class:

public class Test5 {

public static void main (String args []) {

/* This is the start of a comment

if (true) {

Test5 = new test5();

System.out.println("Done the test");

}

/* This is another comment */

System.out.println ("The end");

}

}

1) Prints out "Done the test" and nothing else.

2) Program produces no output but terminates correctly.

3) Program does not terminate. 4) The program will not compile.

5) The program generates a runtime exception.

6) The program prints out "The end" and nothing else.

Answer : 6

64. What is the result of compiling and running the following applet:

import java.applet.Applet;

import java.awt.*;

public class Sample extends Applet {

private String text = "Hello World";

public void init() {

add(new Label(text));

}

public Sample (String string) {

text = string;

}

}

It is accessed form the following HTML page:

Sample Applet

1) Prints "Hello World". 2) Generates a runtime error. 3) Does nothing.

Answer : 2

65. What is the effect of compiling and (if possible) running this class:

public class Calc {

public static void main (String args []) {

int total = 0;

for (int i = 0, j = 10; total > 30; ++i, --j) {

System.out.println(" i = " + i + " : j = " + j);

total += (i + j);

}

System.out.println("Total " + total);

}

}

1) Produce a runtime error

2) Produce a compile time error

3) Print out "Total 0"

4) Generate the following as output:

i = 0 : j = 10

i = 1 : j = 9

i = 2 : j = 8

Total 30

Answer : 3

true or false.

a:abstract class maynot contain final method.

b:final class maynot have any abstract

ans:b

A Java object is:

A. An instance of a class. In it there are the data and the methods

which manage these data

B. A variable of any type

C. A particular structure, like a record , in which there are variables

of different types

Two different objects belonging to the same class:

A. Share data

B. Share the structure. They can share data too, through the static

keyword

C. Share data and methods

D. Share only methods

A class is:

A. A collection of objects. The linking of objects represents the

Encapsulation

B. type from which you can instantiate objects

C. The implementation of an object in Java syntax

Inheriting a class B from another class A:

A. B inherits all members from A. It's possible to exclude from

inheriting some members with the static keyword

B. B inherits all members from A. It's possible to exclude from

inheriting some members with the protected keyword

C. You instantiate different objects with same structure. It's the

implementation of "polymorphism",one of the milestone of OO

programming

D. B inherits all attrubutes and methods from A, but it can access

only the public, protected and package ones

What is function overloading?

A. Happens when you declare too many functions in a Java block of

code. In java this limit is fixed to 65536 functions

A. It's the assigning of many tasks to one (or more) methods. It's an

OOP milestone

B. It's the assigning of the same name to many methods. It can be

useful in presence of similar methods which behaves differently

or have different parameters

What is method overriding?

A. An overloading alias

B. It doesn't exists, you invented it

C. Redefining a method inherited from superclass, you specify its

tasks

D. Redefining a method inherited from superclass, you generalize its

tasks

If class C extends B which in turn extends A and c, b, a are three instances of respective classes, which of next sentences are true:

A. I can assign b to a

B. I can assign a to b

C. I can assign c to a

D. I can assign a to c

E. To an Object handle o I can assign a or b or c

Supposing in the AWT package (Abstract Window Toolkit) exists a graphic class "Button", what happens in this code fragment?

1.

2. Button p,q;

3. p = new Button ("Ok");

4. q = p;

5. q.setLabel("Cancel");

6.

A. Two buttons, with "OK" and "Cancel" labels

B. Two overlapping buttons, the last with "Cancel" label

C. Two buttons, with "Cancel" and "Cancel" labels

D. Only one button, with "Cancel" label

Supposing you have a class hierarchy with a "Shape" superclass, What does this code fragment do?

1. public abstract class Shape{

2.

3. public void draw(int anX, int anY){...}

4. }

5.

6. public class Square extends Shape{

7. public void draw(int unX, int unY){\\ draw a square}

8. }

9.

10. public class Circle extends Shape{

11. public void draw(int unX, int unY){\\ draw a circle}

12. }

13.

14. Square q = new Circle(x, y);

15. q.draw();

16.

A. It's a typical example of polymorphism, but you should declare q

of Shape type, not Square.

B. It's a typical example of polymorphism. Being Square and Circle

both of Shape type, the handles are compatible, and the

mechanism of dynamic binding choose the method at run-time

instead of compile-time

C. It's a typical example of polymorphism, but you should declare q

of Circle type, not Square.

What happens in this code fragment?

1.

2. String a = new String("Test");

3. String b = new String("test");

4. if (a==b)

5. System.out.println(" Same");

6. else

7. System.out.println(" Different");

A. Prints "Different", because a and b points to strings which are

different for letter casing, and Java is case-sensitive

B. Prints "Same", because the "==" operator is overridden in the

String class

C. Prints "Different", and it should do the same with any value

D. Prints "Same", and it should do the same with any value

Which of the following are collection classes?
1) Collection 2) Iterator 3) HashSet 4) Vector

Which of the following are true about the Collection interface?
1) The Vector class has been modified to implement Collection
2) The Collection interface offers individual methods and Bulk methods such as addAll
3) The Collection interface is backwardly compatible and all methods are available within the JDK 1.1 classes
4) The collection classes make it unnecessary to use arrays

Which of the following are true?
1) The Set interface is designed to ensure that implementing classes have unique members
2) Classes that implement the List interface may not contain duplicate elements
3) The Set interface is designed for storing records returned from a database query
4) The Map Interface is not part of the Collection Framework

Which of the following are true?

Which method can be used to compare two strings for equality?

Which method can be used to perform a comparison between strings that ignores case differences?

What is the use of valueOf( ) method?

What are the uses of toLowerCase( ) and toUpperCase( ) methods?

Which method can be used to find out the total allocated capacity of a StrinBuffer?

Which method can be used to set the length of the buffer within a StringBuffer object?

What is the difference between String and StringBuffer?

What are wrapper classes?

Which of the following is not a wrapper c lass?

a. String Integer Boolean Character

You wish to store a small amount of data and make it available for rapid access. You do not have a need for the data to be sorted, uniqueness is not an issue and the data will remain fairly static Which data structure might be most suitable for this requirement?


1) TreeSet 2) HashMap 3) LinkedList 4) an array

Which of the following are Collection classes?

1) ListBag 2) HashMap 3) Vector 4) SetList

How can you remove an element from a Vector?


1) delete method 2) cancel method 3) clear method 4) remove method

Questions on Language Fundamentals

1. Which of these are legal identifiers. Select all the correct answers.

A. number_1

B. number_a

C. $1234

D. -volatile

2. Which of these are not legal identifiers. Select all the correct answers.

A. 1alpha

B. _abcd

C. xy+abc

D. transient

E. account-num

F. very_long_name

3. Which of the following are keywords in Java. Select all the correct answers.

A. friend

B. NULL

C. implement

D. synchronized

E. throws

4. Which of the following are Java keywords. Select all the correct answers.

A. super

B. strictfp

C. void

D. synchronize

E. instanceof

5. Which of these are Java keywords. Select all the correct answers

A. TRUE

B. volatile

C. transient

D. native

E. interface

F. then

G. new

6. Using up to four characters, write the Java representation of octal literal 6.

7. Using up to four characters, write the Java representation of integer literal 3 in hexadecimal.

8. Using up to four characters, write the Java representation of integer literal 10 in hexadecimal.

9. What is the minimum value of char type. Select the one correct answer.

A. 0

B. -215

C. -28

D. -215 - 1

E. -216

F. -216 - 1

10. How many bytes are used to represent the primitive data type int in Java. Select the one correct answer.

A. 2

B. 4

C. 8

D. 1

E. The number of bytes to represent an int is compiler dependent.

11. What is the legal range of values for a variable declared as a byte. Select the one correct answer.

A. 0 to 256

B. 0 to 255

C. -128 to 127

D. -128 to 128

E. -127 to 128

F. -215 to 215 - 1

12. The width in bits of double primitive type in Java is --. Select the one correct answer.

A. The width of double is platform dependent

B. 64

C. 128

D. 8

E. 4

13. What would happen when the following is compiled and executed. Select the one correct answer.

public class Compare {

public static void main(String args[]) {

int x = 10, y;

if(x <>

y = 1;

if(x>= 10) y = 2;

System.out.println("y is " + y);

}

}

A. The program compiles and prints y is 0 when executed.

B. The program compiles and prints y is 1 when executed.

C. The program compiles and prints y is 2 when executed.

D. The program does not compile complaining about y not being initialized.

E. The program throws a runtime exception.

14. What would happen when the following is compiled and executed. Select the one correct answer.

class example {

int x;

int y;

String name;

public static void main(String args[]) {

example pnt = new example();

System.out.println("pnt is " + pnt.name +

" " + pnt.x + " " + pnt.y);

}

}

A. The program does not compile because x, y and name are not initialized.

B. The program throws a runtime exception as x, y, and name are used before initialization.

C. The program prints pnt is 0 0.

D. The program prints pnt is null 0 0.

E. The program prints pnt is NULL false false

15. The initial value of an instance variable of type String which is not explicitly initialized in the program is --. Select the one correct answer.

A. null

B. ""

C. NULL

D. 0

E. The instance variable must be explicitly assigned.

16. The initial value of a local variable of type String which is not explicitly initialized and which is defined in a member function of a class. Select all the correct answer.

A. null

B. ""

C. NULL

D. 0

E. The local variable must be explicitly assigned.

17. Which of the following are legal Java programs. Select all the correct answer.

A. // The comments come before the package
package pkg;
import java.awt.*;
class C{};

B. package pkg;
import java.awt.*;
class C{};

C. package pkg1;
package pkg2;
import java.awt.*;
class C{};

D. package pkg;
import java.awt.*;

E. import java.awt.*;
class C{};

F. import java.awt.*;
package pkg;
class C {};

18. Which of the following statements are correct. Select all correct answers.

A. A Java program must have a package statement.

B. A package statement if present must be the first statement of the program

C. If a Java program defines both a package and import statement, then the import statement must come before the package statement.

D. An empty file is a valid source file.

E. A Java file without any class or interface definitions can also be compiled.

F. If an import statement is present, it must appear before any class or interface definitions.

19. What would be the results of compiling and running the following class. Select the one correct answer.

class test {

public static void main() {

System.out.println("test");

}

}

A. The program does not compile as there is no main method defined.

B. The program compiles and runs generating an output of "test"

C. The program compiles and runs but does not generate any output.

D. The program compiles but does not run.

20. Which of these are valid declarations for the main method? Select all correct answers.

A. public void main();

B. static void main(String args[]);

C. public static void main(String args[]);

D. static public void main(String);

E. public static void main(String );

F. public static int main(String args[]);

21. Which of the following are valid declarations for the main method. Select all correct answers.

A. public static void main(String args[]);

B. public static void main(String []args);

C. final static public void main (String args[]);

D. public static int main(String args[]);

E. public static abstract void main(String args[]);

22. What happens when the following program is compiled and executed with the arguments - java test. Select the one correct answer.

class test {

public static void main(String args[]) {

if(args.length > 0)

System.out.println(args.length);

}

}

A. The program compiles and runs but does not print anything.

B. The program compiles and runs and prints 0

C. The program compiles and runs and prints 1

D. The program compiles and runs and prints 2

E. The program does not compile.

23. What is the result of compiling and running this program ? Select the one correct answer.

public class test {

public static void main(String args[]) {

int i, j;

int k = 0;

j = 2;

k = j = i = 1;

System.out.println(k);

}

}

A. The program does not compile as k is being read without being initialized.

B. The program does not compile because of the statement k = j = i = 1;

C. The program compiles and runs printing 0.

D. The program compiles and runs printing 1.

E. The program compiles and runs printing 2.

24. What gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select the one correct answer.

public class test {

public static void main(String args[]) {

System.out.println(args[0] + " " + args[args.length - 1]);

}

}

A. The program will throw an ArrayIndexOutOfBounds exception.

B. The program will print "java test"

C. The program will print "java hapens";

D. The program will print "test happens"

E. The program will print "lets happens"

25. What gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select the one correct answer.

public class test {

public static void main(String args[]) {

System.out.println(args[0] + " " + args[args.length]);

}

}

A. The program will throw an ArrayIndexOutOfBounds exception.

B. The program will print "java test"

C. The program will print "java hapens";

D. The program will print "test happens"

E. The program will print "lets happens"

26. What all gets printed on the standard output when the class below is compiled and executed by entering "java test lets see what happens". Select all correct answers.

public class test {

public static void main(String args[]) {

System.out.println(args[0] + " " + args.length);

}

}

A. java

B. test

C. lets

D. 3

E. 4

F. 5

G. 6

27. What happens when the following program is compiled and run. Select the one correct answer.

public class example {

int i = 0;

public static void main(String args[]) {

int i = 1;

i = change_i(i);

System.out.println(i);

}

public static int change_i(int i) {

i = 2;

i *= 2;

return i;

}

}

A. The program does not compile.

B. The program prints 0.

C. The program prints 1.

D. The program prints 2.

E. The program prints 4.

28. What happens when the following program is compiled and run. Select the one correct answer.

public class example {

int i = 0;

public static void main(String args[]) {

int i = 1;

change_i(i);

System.out.println(i);

}

public static void change_i(int i) {

i = 2;

i *= 2;

}

}

A. The program does not compile.

B. The program prints 0.

C. The program prints 1.

D. The program prints 2.

E. The program prints 4.

29. What happens when the following program is compiled and run. Select the one correct answer.

public class example {

int i[] = {0};

public static void main(String args[]) {

int i[] = {1};

change_i(i);

System.out.println(i[0]);

}

public static void change_i(int i[]) {

i[0] = 2;

i[0] *= 2;

}

}

A. The program does not compile.

B. The program prints 0.

C. The program prints 1.

D. The program prints 2.

E. The program prints 4.

30. What happens when the following program is compiled and run. Select the one correct answer.

public class example {

int i[] = {0};

public static void main(String args[]) {

int i[] = {1};

change_i(i);

System.out.println(i[0]);

}

public static void change_i(int i[]) {

int j[] = {2};

i = j;

} }

A. The program does not compile.

B. The program prints 0.

C. The program prints 1.

D. The program prints 2.

E. The program prints 4.


Answers to questions on Language Fundamentals

1. a, b, c

2. a, c, d, e

3. d, e

4. a, b, c, e

5. b, c, d, e, g

6. Any of the following are correct answers - 06, 006, or 0006

7. Any of the following are correct answers - 0x03, 0X03, 0X3 or 0x3

8. Any of the following are correct answers - 0x0a, 0X0a, 0Xa, 0xa, 0x0A, 0X0A, 0XA, 0xA

9. a

10. b

11. c

12. b

13. d. The variable y is getting read before being properly initialized.

14. d. Instance variable of type int and String are initialized to 0 and NULL respectively.

15. a

16. e

17. a, b, d, e

18. b, d, e, f

19. d

20. b, c

21. a, b, c

22. a

23. d

24. e

25. a

26. c, e

27. e

28. c

29. e

30. c

0 Comments: