Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
class Base{
public static void main(String[] args){
System.out.println("Hello");
}
}
ANS : 4
QUESTION2
What is the result of trying to compile and run the following code.
public final static void main(String[] args){
double d = 10.0 / -0;
if(d == Double.POSITIVE_INFINITY)
System.out.println("Positive infinity");
else
System.out.println("Negative infinity");
}
ANS : 1
QUESTION3
1. 2
2. 0
3. 3
4. 2.5
5. 25
ANS : 2
The result of 1/4 will be zero because integer
divion is carried out on the operands.
If you need to obtain a fractional value
you need to use either a float or double literal
as in 1F / 4F.
QUESTION4
1. int i = 0XCAFE;
2. boolean b = 0;
3. char c = 'A';
4. byte b = 128;
5. char c = "A";
ANS : 1,3
QUESTION5
1. Compiler error.
2. Compiles and runs printing out 2
3. Compiles and runs printing out 1
4. An ArrayIndexOutOfBounds Exception at runtime
ANS : 2
QUESTION6
1. Compiler error.
2. Will throw a NoSuchMethod error at runtime.
3. It will compile and run printing out "10"
4. It will run with no output.
5. It will run and print "10" and then crash with an error.
ANS : 3
QUESTION7
Is this legal?
long longArr[];
int intArr[] = { 7 ,8 , 9};
longArr = intArr;
1. Yes
2. No
ANS : 2
QUESTION8
True or False.
The range of a byte is from -127 to 128
1. True
2. False
ANS : 2
Correct answer/s : 2
The statement is false. The range of an array
is from - 128 to 127
QUESTION9
1. float f = \u0038;
2. long L2 = 2L;
3. float f = 1.2;
4. char c = '/u004E';
5. byte b = 100;
ANS : 1,2,4,5
QUESTION10
What is the result of trying to compile and run the following code.
public static void main(String[] args){
double d = 10 / 0;
if(d == Double.POSITIVE_INFINITY)
System.out.println("Positive infinity");
else
System.out.println("Negative infinity");
}
1. output Positive infinity
2. output Negative infinity
3. Will fail to compile
4. Runtime exception
ANS : 4
ANS : 4
QUESTION : 2
ANS : 5
1. Is wrong because it is legal to assign integer
literal to a char variable as long as the value
does not exceed the range of a char.
2. Is wrong because parseByte(String s , int radix)
will accept any native numeric type that is not
wider than an int.
3 and 6 are just nonsense.
4. Is wrong because the the character b falls within
the radix range specified by the second parameter.
QUESTION : 3
1. 22
2. 22.0
3. -22
4. -23
5. -22.0
6. 23.0
ANS : 5
QUESTION : 4
ANS : 4
QUESTION : 5
ANS : 2,4,5
QUESTION : 6
QUESTION : 7
1. Compiler error
2. Runtime error
3. Runs and prints "false"
4. Runs and prints "0"
5. Runs and prints "1"
6. Runs adn prints "-1"
ANS : 1
correct answer/s : 1
QUESTION : 8
1. Compiler error
2. Runtime error
3. Runs and prints "false"
4. Runs and prints "true"
ANS : 2
QUESTION : 9
What is the result of attempting to compile and run this ?
1. Compiler error
2. Runtime error
3. Runs and prints "false"
4. Runs and prints "true"
ANS : 1
QUESTION : 10
1. Compiler error
2. Runtime error
3. Runs and prints "256"
4. Runs and prints "0"
5. Runs and prints "127"
ANS : 4
QUESTION1
ANS : 1
QUESTION2
1. Yes
2. No
ANS : 2
Local variables cannot be declared static.
QUESTION3
Is this legal
public class Test {
static { int a = 5; }
public static void main(String[] args){
System.out.println(a);
}
}
1.Yes
2.No
ANS : 2
Correct ans : 2
A variable declared in a static initialiser is not accessible outside its
enclosing block.
QUESTION4
ANS : 2,4,5,6
QUESTION5
1. char c = 'a';
2. char c = '\'';
3. char c = '\n';
4. char c = "a";
5. char c = \u0061;
6 char c = 97;
ANS : 1
QUESTION6
1. boolean b = True;
2. boolean b = 0;
3. boolean b = 1 < 2;
4. boolean b = true?false:true;
5. boolean b = true;
ANS : 3,4,5
QUESTION7
1. int i = 16.0;
2. byte b = 16.0;
3. float f = 16.0;
4. double d = 16.0;
5. char \u0063 = '\u0063';
ANS : 4,5
16.0 is a double type literal hence it becomes a widening
assignment for a byte, int or float.
QUESTION8
ANS : 1
Numeric instance variables are always
automatically initialised to its default value.
So you can use them without explicitly initialising them.
QUESTION9
1. Compiler error
2. Runtime exception
3. Compiles and runs printing out 0
ANS : 3
QUESTION10
ANS : 5
class Mammal{
void eat(Mammal m){
System.out.println("Mammal eats food");
}
}
class Cattle extends Mammal{
void eat(Cattle c){
System.out.println("Cattle eats hay");
}
}
ANS : 1
QUESTION : 2
ANS : 1
QUESTION : 3
What will happen if you try to compile and run this ?
interface A{
public void innerMeth();
}
public class Test {
A a;
int memVar = 1;
void aMethod(){
a = new A(){
public void innerMeth(){
System.out.println(memVar);
} };
}
1. Compiler error.
2. NoSuchMethodException at runtime.
3. Compiles and runs printing 1
4. Throws a NullPointerException at runtime.
ANS : 4
You will get a NullPointerException because the
inner class object gets assigned to the reference a
only after the aMethod() runs. You can prevent
the exception by calling t.aMethod() before the
inner anonymous class method is called.
QUESTION : 4
What will happen if you try to compile and run this code.
class Rectangle{
public int area(int length , int width) {
return length * width;
}
}
ANS : 1
class Base{}
class Derived extends Base{}
public class Test {
public static void main(String[] args){
Derived d = (Derived) new Base();
}
}
ANS : 3
QUESTION : 6
1. 10
2. 20
3. 30
4. 40
ANS : 4
QUESTION : 7
class Base{
static int value = 0;
Base(){
addValue();
}
static void addValue(){
value += 10;
}
int getValue(){
return value;
}
}
class Derived extends Base{
Derived(){
addValue();
}
static void addValue(){
value += 20;
}
}
public class Test {
public static void main(String[] args){
Base b = new Derived();
System.out.println(b.getValue());
}
}
1. 10
2. 20
3. 30
4. 40
ANS : 3
QUESTION : 8
ANS : 1
QUESTION : 9
class Base{
String s = "Base";
String show(){
return s;
}
}
class Derived extends Base{
String s = "Derived";
}
public class Test {
void print(Base b){
System.out.println(b.show());
}
void print(Derived d){
System.out.println(d.show());
}
public static void main(String[] args){
Test t = new Test();
Base b = new Derived();
t.print(b);
}
}
ANS : 4
QUESTION : 10
ANS : 3
QUESTION1
What is the result of attempting to compile and run this code ?
ANS : 1
QUESTION2
1. Compiler error
2. It will print "Error A"
3. It will print "Error B"
4. The exception will go uncaught by both catch blocks
ANS : 1
QUESTION3
1. Yes
2. No
ANS : 1
The boolean condition here evaluates to true
so the print line will execute once.
QUESTION4
ANS : 3
QUESTION5
ANS : 1
QUESTION6
1. Prints - Default , A , B , C
2. Prints - A
3. Prints - B , C
4. Prints - A, B , C , Default
5. Prints - Default
ANS : 3
QUESTION7
1. 64
2. 53
3. 76
4. 0
ANS : 4
QUESTION8
1. Yes
2. No
ANS : 2
QUESTION9
Is this legal ?
class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}
public class Test{
void thrower() throws ExceptionA{
throw new ExceptionA();
}
public static void main(String[] args){
Test t = new Test();
try{t.thrower();}
catch(ExceptionB e) {}
}
}
1. Yes
2. No
ANS : 2
correct answer/s : 2
QUESTION10
Is this legal ?
class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}
class A{
void thrower() throws ExceptionA{
throw new ExceptionA();
}
}
public class B extends A{
void thrower() throws ExceptionB{
throw new ExceptionB();
}
}
1. Yes
2. No
ANS : 1
QUESTION : 1
What will be the result of compiling and running the following code?
import java.awt.*;
import java.applet.*;
public class AppletTest extends Applet {
Label lbl = new Label("hello");
public void init()
{
setSize(200,100);
setVisible(true);
lbl.setBackground(new Color(0,100,180));
setLayout(new GridLayout(1,1));
add(lbl);
setLayout(new FlowLayout());
lbl.setBounds(0,0,100,24);
}
}
1. The label will fill half the display area of the applet.
2. The label will be wide enough to display the text "hello"
3. The label will not be visiblle.
4. The label will fill the entire display area of the applet
5. The code will throw a run time error because of the second setLayout() call.
ANS : 2
QUESTION : 2
What will be the result of compiling and running the following applet?
import java.awt.*;
import java.applet.*;
public class AppletTest extends Applet {
public void init()
{
super.init();
PanelTest p = new PanelTest();
p.init();
setVisible(true);
setSize(200,100);
add(p);
}
1. The button will fill the entire display area of the applet.
2. The code will fail to compile.
3. The button will be just big enough to encompass it's label.
4. The applet's display area will be blank.
ANS : 2
QUESTION : 3
This code compiles without error.
class MyButton extends Button implements MouseListener{
public MyButton(String lbl) {
super(lbl);
addMouseListener(this);
}
public void mousePressed(MouseEvent e){
//do something
}
}
1. False
2. True
ANS : 1
QUESTION : 4
1. TextField();
2. TextField(int rows , int cols);
3. TextField(int cols , String txt);
4. TextField(int cols);
5. TextField(String txt , boolean scrollBars);
ANS : 1,4
QUESTION : 5
True or false?
void setResizable(boolean) is a member of the Applet class.
1. True
2. False
ANS : 2
QUESTION : 6
ANS : 1
QUESTION : 7
1. FlowLayout
2. GridLayout
3. CardLayout
4. BorderLayout
5. GridBagLayout
ANS : 4
QUESTION : 8
True or false.
A Dialog is a subclass of Frame.
1. True
2. False
ANS : 2
QUESTION : 9
1. MenuItem()
2. MenuItem(String name)
3. MenuItem(String name , boolean removable)
4. MenuItem(String name , MenuShortcut sc)
5. MenuItem(boolean check)
ANS : 1,2,4
QUESTION : 10
ANS : 3
QUESTION : 1
You can create a RandomAccessFile object in the following manner
FileInputStream fis = new FileInputStream("file path");
RandomAccessFile raf = new RandomAccessFile(fis , "rw");
1. False.
2. True
ANS : 1
QUESTION : 2
1. 1
2. 0
ANS : 1
QUESTION : 3
ANS : 4
QUESTION : 4
ANS : 2,4
QUESTION : 5
True or false.
UTF characters are always 16 bits
1. True
2. False
ANS : 2
QUESTION : 6
void aMethod(){
File f = new File("test.txt");
try{ f.createNewFile();}
catch(Exception e){System.out.println("err");}
}
1. True
2. False
ANS : 2
QUESTION : 7
Which of the following are valid constructors for
a DataInputStream ?
1. DataInputStream(FileInputStream fis)
2. DataInputStream(DataInputStream dis)
3. DataInputStream(File f)
4. DataInputStream(FilterInputStream fis)
ANS : 1,2,4
QUESTION : 8
True or false ?
1. True
2. False
ANS : 2
QUESTION : 9
True or false?
1. True
2. False
ANS : 1
QUESTION : 10
ANS : 1
QUESTION : 1
What is the result of attempting to compile and run the following code?
ANS : 3
correct answer/s : 3
QUESTION : 2
What is the result of attempting to compile and run the following code?
public class Test {
ANS : 1
correct answer/s : 1
QUESTION : 3
What is the result of attempting to compile and run the following code?
public class Test {
ANS : 4
correct answer/s : 4
QUESTION : 4
1. 8 , 4 and 4
2. 4 , 4 and 8
3. 8 and 4
4. 4 and 8
ANS : 1
correct answer/s : 1
QUESTION : 5
ANS : 1,4
QUESTION : 6
ANS : 3,5
QUESTION : 7
ANS : 1,2,3
QUESTION : 8
True or False.
A WeakHashMap is synchronized.
1. True
2. False
ANS : 2
correct answer/s : 2
QUESTION : 9
True or False.
A Set rejects duplicates and is ordered
1. True
2. False
ANS : 2
correct answer/s : 2
QUESTION : 10
ANS : 1,2,3,4,5,6
1. 0
2. -256
3. 127
4. -127
5. -1
ANS : 2
QUESTION : 2
1. 32
2. 12
3. 8
4. 24
5. 128
6. 0
ANS : 5
QUESTION : 3
1. 0
2. 1
3. 2
4. 3
ANS : 1
QUESTION : 4
ANS : 1
QUESTION : 5
True or False.
The garbage collector is required to makes sure that all objects
held by soft references are garbage collected before the VM throws
an OutOfMemoryError.
1. True
2. False
ANS : 1
QUESTION : 6
1. 4
2. 8
3. 12
4. 0
ANS : 3
QUESTION : 7
1. 0
2. 4
3. 6
4. 8
5. 12
ANS : 5
QUESTION : 8
ANS : 3
QUESTION : 9
Is this legal ?
1. No.
2. Yes.
ANS : 2
QUESTION : 10
1.int i = (int)16.2d;
2. byte b = (byte)(long)16.2;
3. byte b = 128;
4. float f = 16.2;
5. byte b = (int)16.2;
ANS : 1,2,5
QUESTION : 1
1. True.
2. False.
ANS : 2
QUESTION : 2
True or false ?
1. True
2. False
ANS : 2
QUESTION : 3
A number of threads of the same priority have relinquished the lock
on a monitor and are in a waiting state after having called the wait()
method of the object. A new thread enters the monitor and calls the
notifyAll() method of the meonitor. Which of these threads will be the
first one to resume?
ANS : 3
QUESTION : 4
ANS : 2,3,4
QUESTION : 5
1. True
2. False
ANS : 1
QUESTION : 6
1. True
2. False
ANS : 2
QUESTION : 7
ANS : 1,2,5,6
QUESTION : 8
ANS : 2
QUESTION : 9
True or false?
A Thread object has a method called notify().
1. False
2. True
ANS : 2
QUESTION : 10
Calling the destroy() method of a thread object relases all the locks held by
the thread ?
1. True
2. False
ANS : 2