Font size
WorksheetsChapter1
Total questions: 20
Worksheet time: 27mins
What will happen when you compile and run the following code?
}
}
1 will be printed out
2 will be printed out
A compile time error will be generated
An exception will be thrown
Which will be compiled
double a = 100.0;
double a = 100_.0;
double a = 1_000.0;
double a = _100.0;
Which will be compiled
int val = null;
int val = 1;
int val = 1;
int len = val.length();
String val = "hello";
int len = val.length();
Which will be compiled
String main;
String $val;
String 1val;
String const;
Which of the following legally fill in the blank so you can run the main() method from the command line?
public static void main(______)
String _name []
String [] 123
String ... abc
String [] $n
Which method declarations will enable a class to be run as a standalone program?
static void main(String args[ ])
public void static main(String args[ ])
public static main(String[ ] argv)
final public static void main(String [ ] array)
public static void main(String args[ ])
What will the following code print when run?
Hello
It will not print anything.
It will not compile
It will throw an Error at runtime.
None of the above.
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
Given the code in the picture
Which of the following commands will compile and then print "hello"?
javac MyFirstClass
java MyFirstClass hello hello
javac MyFirstClass.java
java MyFirstClass hello hello
javac MyFirstClass
java MyFirstClass hello
javac MyFirstClass.java
java MyFirstClass hello
The following options show the complete code listings of a file. Which of these will compile?
Only one of the above options is correct.
What will be printed when the above class is run using the following command line:
java ArgsPrinter 1 2 3 4
1 2 3
ArgsPrinter 1 2
java ArgsPrinter 1 2
None of the above.
1 1 1
What will be the result of attempting to compile this class?
The class will fail to compile, since the class OtherClass is used before it is defined.
There is no problem with the code.
The class will fail to compile, since the class OtherClass must be defined in a file called OtherClass.java
The class will fail to compile .
None of the above.
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?
Replace Logger logger = new Logger();
with:
log4j.Logger logger = new log4j.Logger();
Replace package util.log4j;
with:
package util;
Replace Logger logger = new Logger();
with:
util.log4j.Logger logger = new util.log4j.Logger();
Remove package util.log4j; from Logger.
Add import log4j; to TestClass.
Which of the given options can be successfully inserted at line 1
import java.lang.*
package p.util;
public class MyClass{ }
abstract class MyClass{ }
What will be printed if the program is run using the command line:
java TestClass 111 222 333
111
222
333
It will throw an ArrayIndexOutOfBoundsException
None of the above.
Given the following class, which of these are valid ways of referring to the class from outside of the package com.enthu?
Base
By importing the package com.* and referring to the class as enthu.Base
By importing com.enthu.* and referring to the class as Base.
By referring to the class as com.enthu.Base.
The following are the complete contents of TestClass.java file.
Which packages are automatically imported?
What will the following code print when compiled and run?
111 333 333
333 333 333
java.lang.NumberFormatException
java.lang.Exception
Compilation fails.
Consider the following code appearing in a file named TestClass.java
Which of the lines are invalid?
// 1 and // 4
// 3 and // 4
// 2 and // 4
// 2 and // 3
