wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Chapter1

Total questions: 20

Worksheet time: 27mins

Name
Class
Date
1.

What will happen when you compile and run the following code?

}

}

a)

1 will be printed out

b)

2 will be printed out

c)

A compile time error will be generated

d)

An exception will be thrown

2.

Which will be compiled

a)

double a = 100.0;

b)

double a = 100_.0;

c)

double a = 1_000.0;

d)

double a = _100.0;

3.

Which will be compiled

a)

int val = null;

b)

int val = 1;

c)

int val = 1;

int len = val.length();

d)

String val = "hello";

int len = val.length();

4.

Which will be compiled

a)

String main;

b)

String $val;

c)

String 1val;

d)

String const;

5.

Which of the following legally fill in the blank so you can run the main() method from the command line?

public static void main(______)

a)

String _name []

b)

String [] 123

c)

String ... abc

d)

String [] $n

6.

Which method declarations will enable a class to be run as a standalone program?

a)

static void main(String args[ ])

b)

public void static main(String args[ ])

c)

public static main(String[ ] argv)

d)

final public static void main(String [ ] array)

e)

public static void main(String args[ ])

7.

What will the following code print when run? 

a)

Hello

b)

It will not print anything.

c)

It will not compile

d)

It will throw an Error at runtime.

e)

None of the above.

8.

The options below contain the complete contents of a file. Which of these options can be run with the following command line once compiled? java main

a)
b)
c)
d)
9.

Which of the following lines can be inserted at line 1 to make the program run?

(Assume that PrintWriter and OutputStreamWriter are valid classes in java.io package)

a)

import java.lang.*;

b)

import java.io.*;

c)

import java.io.OutputStreamWriter;

d)

include java.io.*;

10.

Given the code in the picture

Which of the following commands will compile and then print "hello"?

a)

javac MyFirstClass

java MyFirstClass hello hello

b)

javac MyFirstClass.java

java MyFirstClass hello hello

c)

 

javac MyFirstClass

java MyFirstClass hello

 

d)

javac MyFirstClass.java

java MyFirstClass hello

11.

The following options show the complete code listings of a file. Which of these will compile?

a)
b)
c)
d)
e)

Only one of the above options is correct.

12.

What will be printed when the above class is run using the following command line:

java ArgsPrinter 1 2 3 4

a)

1 2 3

b)

ArgsPrinter 1 2

c)

java ArgsPrinter 1 2

d)

None of the above.

e)

1 1 1

13.

What will be the result of attempting to compile this class?

a)

The class will fail to compile, since the class OtherClass is used before it is defined.

b)

There is no problem with the code.

c)

The class will fail to compile, since the class OtherClass must be defined in a file called OtherClass.java

d)

The class will fail to compile .

e)

None of the above.

14.

Given the following contents of two java source files in the picture.

What changes, when made independently, will enable the code to compile and run?

a)

Replace Logger logger = new Logger();

with:

log4j.Logger logger = new log4j.Logger();

 

 

b)

Replace package util.log4j;

with:

package util;

c)

Replace Logger logger = new Logger();

with:

util.log4j.Logger logger = new util.log4j.Logger();

d)

Remove package util.log4j; from Logger.

e)

Add import log4j; to TestClass.

15.

Which of the given options can be successfully inserted at line 1

a)

import java.lang.*

b)

package p.util;

c)

public class MyClass{ }

d)

abstract class MyClass{ }

16.

What will be printed if the program is run using the command line: 

java TestClass 111 222 333

a)

111

b)

222

c)

333

d)

It will throw an ArrayIndexOutOfBoundsException

e)

None of the above.

17.

Given the following class, which of these are valid ways of referring to the class from outside of the package com.enthu?  

a)

Base

b)

By importing the package com.* and referring to the class as enthu.Base

c)

By importing com.enthu.* and referring to the class as Base.

d)

By referring to the class as com.enthu.Base.

18.

The following are the complete contents of TestClass.java file.

Which packages are automatically imported?

a)

java.util

b)

System

c)

java.lang

e)

The package with no name.

19.

What will the following code print when compiled and run?  

a)

111 333 333

b)

333 333 333

c)

java.lang.NumberFormatException

d)

java.lang.Exception

e)

Compilation fails.

20.

Consider the following code appearing in a file named TestClass.java

Which of the lines are invalid?

a)

// 1 and // 4

b)

// 3 and // 4

c)

// 2 and // 4

d)

// 2 and // 3