Bscit-5 JAVA

JAVA

1.which of the following is a valid main() method?
a) Public static void main()
b) Public static void main(String []s)
c) Void main(String args[])
d) None of the above

2.which element of the array arr is represented in arr[7]?
a) The sixth
b) The seventh
c) The eight
d) None of the above

3.which of the following is a reserved word in java?
a) Template
b) Throwing
c) This
d) Both (b) and (c)

4.which of the following is a primitive data type?
a) Byte
b) String
c) Integer
d) Float

5.the && and || operators
a) compare two numeic values
b) combine two numeric values
c) compare two bookean values
d) combine two Boolean values

6.the break statement causes an exit
a) only from the innermost loop
b) only from the innermost switch
c) from all loops and switches
d) from the innermost loops or switch

7.what is the output of the following program segment?
For(I=1;j=0;I<10,I++)
System.out.println(i);
a) 10
b) 11
c) 55
d) 45

8.classes are useful because they
a) permit data to be hidden from other classes
b) can closely model objects in the real world
c) brings together all aspects of an entity in one place
d) all of the above

9.In java, the main()method returns a value of type
a) void
b) int
c) real
d) string

10.A method parameter is
a) a variable in the method that receives a value frim the calling program.
b) A way that methods resist accepting the calling program’s value.
c) A value sent to the method by the calling program.
d) A value returned to the caller method by the calling program.

11.A default constructor
a) has no return type
b) has no argument
c) has one argument
d) has one argument but no return type

12.the dot operator connects the following two entities:-
a) a class member and a class object
b) a class object and a class
c) a class and a member of that class
d) a class object and a member of that class

13.new operator
a) dynamically allocates storage
b) statically allocates storage
c) allocates storage for a new object
d) none of the above

14.a base class is inherited by a
a) super class
b) derived class
c) constructor
d) none of the above

15.the object-oriented feature that data can be maniputed only through
member methods is
a) inheritance
b) encapsulation
c) polymorphism
d) none of the above

16.private data members can be accessed
a) only from the super class itself
b) both from the super class and from its sub class
c) from the sub class
d) none of the above.

17.class consturctors
a) are private by default
b) are protected by default
c) are public by default
d) have the same access permission as the class

18.what output is displayed as a result of executing the following statement
a) //is this a comment
b) is this a comment
c) no output will be displayed
d) error message will be displayed

19.the first statement in a java source file should be
a) import statement
b) package statement
c) main() method
d) any of the above

20.in java exception handler is invoked with a
a) try block
b) catch block
c) finally block
d) abort()method

21.try
{
int ;
return;
}catch(Exception e){System.out.println(“hi I m om”);
return;
}finally{System.out.println(“hello”);
}
a) hello
b) I m here
c) Hello
d) I m here
e) The program will return without printing anything

22.the exception is processed
a) within the try block
b) throws expression
c) throw expression
d) within the catch block

23.when using java,where does the system stores permanent data select the
correct ans.
a) In stream
b) System.in
c) In files
d) None of the above.

24.select the correct statement:-
a) multithreading is supported by java
b) multithreading requires more than one cpu
c) multithreading requires that a computer has only one cpu
d) none of the above

25.the runnable interface declares the
a) start()method
b) run()method
c) stop()method
d) both a and b

26.which of the following thread state transition is valid?
a) Ready to waiting
b) Ready to running
c) Waiting to running
d) All of the above

27.a dead thread
a) is discarded
b) must wait until all other thread execute before it is restarted
c) can not be restarted
d) is synchronized

28.when using java, where does the system stores permanent data.
Select the correct answer.
a) In streams
b) System.in
c) In files
d) None of the above

29.which of the following will cause the string object referenced by s to tbe
changed
a) s.concat
b) s.toUpperCase()
c) both a and b
d) s.replace()

30._________is a method of the string class
a) delete()
b) append()
c) reverse()
d) replace()

31.which of the following is a valid java identifier
a) %id
b) #_id
c) $id
d) f#if

32. a relational operator in java
a) comprares two operands
b) yields a Boolean result
c) both a and b above
d) none of the above

33.the break statement causes an exit
a) only from the innermost loop
b) only fram the innermost switch
c) from all loops and switches
d) from the innermost loops or switches

34.by using protected access specifier,one can create class members that
a) cannot be inherited and accessed by a derived class
b) can be accessed by a derived class
c) cabn be public
d) none of the above

35.which of the following is the main use of packages in java
a) catergorizez data
b) makes compiling faster
c) resolves name conflicts
d) none of the above

36.which of the following is true about a fibally classe
a) it it only executed after a catch clauses has been executed
b) it is only executed if a catch clause has not been executed
c) it is always executed unless its thread terminates
d) it is only executed if an exception is thrown

37.a dead thread
a) is discarded
b) must wait until all other threads execute before it is restarted
c) cannot be restarted
d) is synchronized

38.the && and || operators
a) compare two numeric values
b) combine two numeric values
c) compare two Boolean values
d) combine two Boolean values

39.a variable defined within a block is visible
a) from the point of definition onwards in the program
b) from the point of definition onwards in the function
c) from the point of definition onwards in the block
d) none of the above

40.new operator in java
a) dynamically allocates storage
b) statically allocates storage
c) allocates storage for a new object
d) none of the above

41.when a thread blocks on i/o, it
a) entrers ready state
b) enters dead state
c) allows no other thread to perform i/o
d) enters waiting state

42.which of the following is correct
a) the input stream and output stream classes are byte oriented
b) the reader and writer classes are byte-oriented
c) the reader and writer classes provides serialized object output
d) none of the above

43.sharing of common information is achieved by the concept of one of the
following
a) polymorphism
b) encapsulation
c) inheritance
d) none of the above

44.which element of the array arr is represented in arr[0]?
a) The first
b) The last
c) Null pointer
d) None of the above

45.the for loop with a multi-statement loop body,semicolons should appear
after
a) the for statement itself
b) the closing brace in a multi-statement loop body
c) each statement within the loop body
d) none of the above

46.classes are useful in java because they
a) permit data to be hidden from other classes
b) can closely model objects in the read world
c) brings together all aspects of an entity in one place
d) all of the above

47.the default constructor in java
a) has no return type
b) has no arguments
c) has one argument
d) has one argument but no return type

48.in java,exception handler is invoked with a
a) try block
b) catch block
c) finally
d) abort()method

49.which of the following thread state transition is valid
a) ready to waiting
b) ready to running
c) waiting to running
d) all of the above

50.when using java, where does the system stores permanent data?select the
correct ans.
a) In stream
b) System.in
c) In files
d) None of the above


JAVA

Q1-The following program is in file Test.java, what would be the output
of compiling and running this program ?
class MyFirstClass{
MyFirstClass(){
System.out.println("MyFirstClass created");
}
}
public class Test{
public static void main(String str[]){
MyFirstClass mf = new MyFirstClass();
MyThirdClass mf = new MyThirdClass();
}
}
public class MyThirdClass{
MyThirdClass(){
System.out.println("MyThirdClass created");
}
}
a) compiles, runs and does not print anything
b) does not compile
c) compiles, runs and prints "MyFirstClass created"
d) compiles, runs and prints "MyFirstClass created" and "MyThirdClass created"

Q2-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test implements Runnable{
public static void main(String str[]){
Thread t = new Thread(new Test());
}
public void run(){
for(int i=0; i<10; i++){
System.out.println("t ");
}
}
}
a) does not compile
b) compiles, runs and prints " t t t t t t t t t t "
c) compiles and does not run
d) compiles, runs and does not print anything

Q3-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String str[]){
System.out.print(5+5+"s"+6+6);
}
}
a) compiles, runs and prints " 10s66 "
b) compiles, runs and prints " 10s12 "
c) compiles, runs and prints " 55s66 "
d) compiles, runs and does not print anything

Q4-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
static private String st;
public static void main(String str[]){
System.out.print(st+"yes");
}
}
a) compiles, runs and prints " yes "
b) does not compiles
c) compiles, runs and prints " nullyes "
d) compiles, runs and prints " null+yes "

Q5-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public Test(float f){
printMessage(f);
}
public void printMessage(float f){
System.out.println("object "+"number "+f+" created");
}
public static void main(String str[]){
Test t1;
Test t2 = new Test(1+9);
t1 = t2;
}
}
a) compiles, runs and prints " object number 10 created "
b) does not compiles
c) compiles, runs and prints " object number 1+9 created "
d) compiles, runs and prints " object number 10.0 created "

Q6-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
Boolean b1 = new Boolean("true");
Boolean b2 = new Boolean(false);
System.out.println(b1);
System.out.println(b2);
}
}
a) compiles, runs and prints
true
false
b) does not compile
c) compiles but runtime error
d) compiles, runs and does not print anything

Q7-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
int i = -1>>>32;
System.out.println(i);
}
}
a) compiles, runs and prints "0"
b) compiles, runs and prints "-1"
c) compiles, runs and prints "1"
d) compiles, runs and prints "-2"

8-The following program is in file Test.java, what would be the output
of compiling and running this program ?
class MyFirstClass{
public MyFirstClass(int k){
System.out.println("we are in MyFirstClass");
}
}
public class Test extends MyFirstClass {
public Test(){
System.out.println(",we are in Test");
}
public static void main(String st[]){
Test t1 = new Test();
}
}
a) compiles, runs and prints ", we are in Test we are in MyFirstClass"
b) compiles, runs and prints "we are in Test"
c) compiles, runs and prints "we are in MyFirstClass,we are in Test"
d) does not compile

Q9-The following program is compiled and run, what is printed
on the Frame when button b2 is clicked?
import java.awt.event.*;
public class Test extends Frame implements ActionListener{
private Button b1;
private Button b2;
private String message="message";
public Test(){
setSize(300,300);
setLayout(new FlowLayout());
b1=new Button("b1");
b2=new Button("b2");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==b1) message=" b1 clicked";
if(ae.getSource()==b2) message=" b2 clicked";
repaint();
}
public void paint(Graphics g){
g.drawString(message,100,100);
}
public static void main(String st[]){
Test t1 = new Test();
}
}
a) "message"
b) "b1 clicked"
c) "b2 clicked"
d) "message b2 clicked"

Q10-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
String s1 = "java";
String s2 = "java";
String s3 = "JAVA";
s2.toUpperCase();
s3.toUpperCase();
boolean b1 = s1==s2;
boolean b2 = s1==s3;
System.out.print(b1);
System.out.print(" "+b2);
}
}
a) compiles, runs and prints "true false"
b) compiles, runs and prints "true true"
c) compiles, runs and prints "false false"
d) compiles, runs and prints "false true"

Q11-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
int j;
bb:
for(int i=1; i<10; i++){
for( j=1; j<3; j++){
if(i>j) break bb;
System.out.print(i+" ");
}
}
}
}
a) compiles, runs and prints "1 1 2 2"
b) compiles, runs and prints "1 1 1 1"
c) compiles, runs and prints "1 2"
d) compiles, runs and prints "1 1"

Q12-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
byte b1 = 23;
byte b2 = 34;
System.out.print(b1*b2);
}
}
a) does not compile
b) compiles, runs and prints "2334"
c) compiles, runs and prints "782"
d) compiles, runs and prints "b1b2"

Q13-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
byte b1 = 23;
byte b2 = 34;
byte b3 = b1*b2;
System.out.print(b3);
}
}
a) does not compile
b) compiles, runs and prints "2334"
c) compiles, runs and prints "782"
d) compiles, runs and prints "b1b2"

Q14-Which of the following codes is(are) ILLEGAL ?
a) interface Int1{
void meth1();
void meth2();
}
public class Test implements Int1{
public void meth1(){}
public void meth2(){}
public static void main(String st[]){
}
}
b) interface Int1{
void meth1();
void meth2();
}
public class Test implements Int1{
void meth1(){}
void meth2(){}
public static void main(String st[]){
}
}
c) interface Int1{
void meth1(){};
void meth2();
}
public class Test implements Int1{
public void meth1(){}
public void meth2(){}
public static void main(String st[]){
}
}
d) interface Int1{
void meth1();
void meth2();
}
public class Test extends Int1{
public void meth1(){}
public void meth2(){}
public static void main(String st[]){
}
}

Q15-Which of the following codes do(es) not compile ?
a) class MyFirstClass {
void meth1(){};
void meth2(){};
}
abstract class MySecondClass {
void meth4();
void meth5();
}
public class Test extends MyFirstClass{
public static void main(String st[]){
}
}
b) class MyFirstClass {
void meth1(){};
void meth2(){};
}
abstract class MySecondClass {
abstract void meth4();
abstract void meth5();
}
public class Test extends MySecondClass{
public static void main(String st[]){
}
}
c) class MyFirstClass {
void meth1(){};
void meth2(){};
}
abstract class MySecondClass {
abstract void meth4();
abstract void meth5();
}
abstract public class Test extends MySecondClass{
public static void main(String st[]){
}
}
d) class MyFirstClass {
void meth1(){};
void meth2(){};
}
abstract class MySecondClass {
abstract void meth4();
bstract void meth5();
}
public class Test extends MyFirstClass{
public static void main(String st[]){
}
}

Q16-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
Test t1 = new Test();
t1.method1(5,0);
}
public void method1(int a, int b){
try{
int c = a/b;
System.out.println("after division");
} catch (ArithmeticException ae){System.out.println("Arithmetic Exception");}
catch (Exception e){System.out.println("Exception");}
finally {System.out.println("Finally");}
System.out.println("after finally");
}
}
a) compiles, runs and prints
after division
Arithmetic Exception
Exception
Finally
after finally
b) compiles, runs and prints
after division
Arithmetic Exception
Finally
after finally
c) compiles, runs and prints
Arithmetic Exception
Finally
after finally
d) compiles, runs and prints
Arithmetic Exception
Exception
Finally
after finally

Q17-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
Test t1 = new Test();
try{
t1.method1(5,0);
} catch (ArithmeticException ae){System.out.println("Arithmetic Exception");}
catch (Exception e){System.out.println("Exception");}
finally {System.out.println("Finally");}
}
public void method1(int a, int b){
int c = a/b;
System.out.println("after division");
}
}
a) compiles, runs and prints
after division
Arithmetic Exception
Exception
Finally
after finally
b) compiles, runs and prints
after division
Arithmetic Exception
Finally
after finally
c) compiles, runs and prints
Arithmetic Exception
Finally
after finally
d) compiles, runs and prints
Arithmetic Exception
Finally
Q18-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
Test t2 = new Test();
t2.method2();
}
public void method2(){
int x=1;
boolean b1=true;
boolean b2=false;
if((b1&b2)&&(++x<4)) System.out.println("x="+x);
if((!b1|b2)||(++x<4)) System.out.println("x="+x);
}
}
a) does not compile.
b) compiles, runs and prints "x=2"
c) compiles, runs and prints "x=1"
d) compiles, runs and prints "x=3"

Q19-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
Test t3 = new Test();
t3.method2();
}
public void method2(){
double x = - 99.5;
System.out.println("output is ="+Math.abs(Math.ceil(Math.floor(x))));
}
}
a) compiles, runs and prints "output is =100.0"
b) compiles, runs and prints "output is =99.5"
c) compiles, runs and prints "output is =100"
d) compiles, runs and prints "output is =-100.0"

Q20-Which of the statements a) b) c) d) would compile if inserted at line 5
import java.io.*;
1: public class Test {
2: public static void main(String st[]) throws IOException{
3: String f="file1.java";
4: File f1 = new File(f);
5: // here
6: }
7: }
a) InputStream iout = new InputStream(f1);
b) InputStream iout = new FileInputStream(f1);
c) FileOutputStream iout = new FileOutputStream(f1);
d) Reader iout = new FileReader(f1);

Q21-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public int x = 99;
private String s = "Hi";
public void method1(){
int x = 66;
class MyClass{
public float y = 45.9f;
public void method2(){
System.out.println("x = "+x);
}
}
MyClass m = new MyClass();
m.method2();
}
public static void main(String st[]){
Test t1 = new Test();
t1.method1();
}
}
a) compiles, runs and prints "99"
b) compiles, runs and prints "66"
c) does not compile
d) compiles but develops a runtime error.

Q22-Which of the statements a) b) c) d) would compile and run
if inserted at line 4.
1: public class Test {
2: public static void main(String st[]){
3: String []s[] = {{"a","b"},{"c","d","e"}};
4: // here
5: }
6: }
a) System.out.println(s[0][1]);
b) System.out.println(s[0][2]);
c) System.out.println(s[1][2]);
d) System.out.println(s[1][3]);

Q23-Which of the statements would NOT compile ?
a) int x = (int)56L;
b) float f = 9.9999;
c) char c =(byte)23;
d) boolean b = False;

Q24-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
int x = 7;
int y = 8;
int z = 2*x|y-1>>2;
System.out.println("z="+z);
}
}
a) does not compile.
b) compiles, runs and prints "z=14"
c) compiles, runs and prints "z=15"
d) compiles, runs and prints "z=16"

Q25-Which of the statements a) b) c) d) would NOT compile if inserted at line 6
1: class MyFirstClass {
2: public int x = 1;
3: private float y = 3.4f;
4: protected String s = "hello";
5: private void meth1(){
6: // here
7: }
8: }
9: public class Test extends MyFirstClass{
10: public void show(){
11: System.out.println(y);
12: }
13: public static void main(String st[]){
14: new Test().show();
15: }
16: }
a) System.out.println(y);
b) System.out.println(x);
c) meth1();
d) System.out.println(s);

Q26-The following program is in file Test.java, what would be the output
of compiling and running this program ?
class MyFirstClass{
public MyFirstClass(){
System.out.println("Hi");
show();
}
public void show(){
System.out.println("in class MyFirstClass");
}
}
public class Test extends MyFirstClass{
public Test(){
System.out.println("Hello");
}
public void show(){
System.out.println("in class Test");
}
public static void main(String st[]){
new Test();
}
}
a) compiles, runs and prints
Hello
Hi
in class MyFirstClass
b) compiles, runs and prints
Hi
Hello
in class MyFirstClass
c) compiles, runs and prints
Hi
in class MyFirstClass
Hello
d) compiles, runs and prints
Hi
in class Test
Hello

Q27-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
int x = 0;
float f = 0.0f;
public Test(int a, float b){
this.x = a;
this.f = b;
}
public static void main(String st[]){
Test t1 = new Test(1,9.9f);
Test t2 = new Test(2,8.8f);
Test t3 = t1.swap(t1,t2);
System.out.println(t1.x+" "+t1.f);
System.out.println(t3.x+" "+t3.f);
}
public Test swap(Test t1, Test t2){
Test te = t1;
t1 = t2;
t2 = te;
Test t3 = new Test(0,0.0f);
t3.x = t2.x;
t3.f = t2.f;
return t3;
}
}
a) compiles, runs and prints
1 9.9
1 9.9
b) compiles, runs and prints
1 9.9
0 0.0
c) compiles, runs and prints
1 9.9
2 8.8
d) compiles, runs and prints
2 8.8
2 8.8

Q28-When you invoke repaint() for a component, which method is called?
a) paint
b) update
c) repaint
d) setVisible
Q29- Which of these names for variables are ILLEGAL ?
a) +var
b) #var
c) $var
d) _9var

Q30-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
String s1 = "JAVAIT";
String s2 = "javaIT";
String s3 = s2.toUpperCase();
boolean b1 = s1.equals(s2);
boolean b2 = s1.equalsIgnoreCase(s2);
boolean b3 = s3.equals(s1);
System.out.println(b1+" "+b2+" "+b3);
}
}
a) compiles, runs and prints "true true true"
b) compiles, runs and prints "false true false"
c) compiles, runs and prints "true false true"
d) compiles, runs and prints "false true true"

Q31-The following program is in file Test.java, what would be the output
of compiling and running this program ?
import java.awt.*;
public class Test extends Frame{
public Test(){
setSize(200,200);
}
public void paint(Graphics g){
g.drawString("Maria",50,50);
}
public static void main(String st[]){
new Test();
}
}
a) compiles, runs and a frame shows up with "Maria" printed
b) compiles, runs, but no frame shows up
c) does not compile
d) compiles but does not run

Q32-The following program is in file Test.java, what would be the output
of compiling and running this program ?
class PrintMess{
synchronized public void show(String mess){
try{
Thread.sleep(1000);
} catch (InterruptedException ie){}
System.out.print(mess);
}
}
class MyThread implements Runnable{
String mess = null;
PrintMess p = null;
Thread t;
public MyThread(PrintMess a, String b){
p = a;
mess = b;
t = new Thread(this);
t.start();
}
public void run(){
for(int i=0; i<5; i++)
p.show(mess);
}
}
public class Test {
public static void main(String st[]){
PrintMess p = new PrintMess();
MyThread m1 = new MyThread(p,"a");
MyThread m2 = new MyThread(p,"b");
}
}
a) compiles, runs and prints "aaaaabbbbb"
b) compiles, runs and prints "ababababab"
c) compiles, runs and prints "bbbbbaaaaa"
d) compiles, runs and prints "baabbabbaa"

Q33-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
l1:
for(int i=4; i<=7; i++)
switch(i){
default:{System.out.println("default");}
case 4: {System.out.println("case 4");}
break;
case 5: {System.out.println("case 5");} break l1;
case 6: {System.out.println("case 6");}
case 7: {System.out.println("case 7");}
}
}
}
a) compiles, runs and prints "default case 4 "
b) compiles, runs and prints "default case 4 case 5"
c) compiles, runs and prints "case 4 case 5"
d) compiles, runs and prints "case 4 case 5 case 6 case 7"

Q34-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
int x = 0;
do {
System.out.println("while");
x = ++x + 1 + x++ ;
}
while(x<=8);
}
}
a) compiles, runs and prints "while while"
b) compiles, runs and prints "while while while"
c) does not compile
d) compiles but does not run

Q35-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
for(int i=0; i<=1 ; i++)
{//j++;
for(int j=0; j<=3; j++)
if(j>=2) break;
System.out.print(i+""+j);
}
}
}
a) compiles, runs and prints "00011011"
b) compiles, runs and prints "0001"
c) compiles, runs and prints "0001101112"
d) does not compile

Q36-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public static void main(String st[]){
int x = 1;
int y = 2;
System.out.println( x == y ? "x equals y" : " x not equal to y" );
}
}
a) compiles, runs and prints "x equals y"
b) compiles, runs and prints "x not equal to y"
c) does not compile
d) compiles, runs and prints "x == y ? "x equals y" : " x not equal to y"

Q37-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
float f = 2.3f;
long l = 234;
public Test(float a, long b){
f = a;
l = b;
}
public static void main(String st[]){
Test t1 = new Test(4.5f , 34);
Test t2 = new Test(6.7f , 234);
Test t3 = new Test(7.6f , 49);
t2 = t1 = t3;
System.out.println(t2.f +" "+ t2.l);
}
}
a) compiles, runs and prints "7.6 49"
b) compiles, runs and prints "6.7 234"
c) does not compile
d) compiles, runs and prints "4.5 34"

Q38-The following program is in file Test.java, what would be the output
of compiling and running this program ?
public class Test {
public void meth1(int b, String s){
System.out.println("meth1 int String");
}
public void meth1(long b, String s){
System.out.println("meth1 long String");
}
public String meth1(long b, String s){
System.out.println("meth1 long String + return ");
return s;
}
public static void main(String st[]){
new Test().meth1(2,"Hi");
}
}
a) does not compile
b) compiles, runs and prints "meth1 int String"
c) compiles, runs and prints "meth1 long String + return"
d) compiles, runs and prints "meth1 long String"
Answers
Answer to Q1: b)
Answer to Q2: d)
Answer to Q3: a)
Answer to Q4: c)
Answer to Q5: d)
Answer to Q6: a)
Answer to Q7: b)
Answer to Q8: d)
Answer to Q9: c)
Answer to Q10: a)
Answer to Q11: d)
Answer to Q12: c)
Answer to Q13: a)
Answer to Q14: b) c) d)
Answer to Q15: a) b)
Answer to Q16: c)
Answer to Q17: d)
Answer to Q18: b)
Answer to Q19: a)
Answer to Q20: b) c) d)
Answer to Q21: c)
Answer to Q22: a) c)
Answer to Q23: b) d)
Answer to Q24: c)
Answer to Q25: a) c)
Answer to Q26: d)
Answer to Q27: a)
Answer to Q28: b)
Answer to Q29: a) b)
Answer to Q30: d)
Answer to Q31: b)
Answer to Q32: b)
Answer to Q33: c)
Answer to Q34: a)
Answer to Q35: d)
Answer to Q36: b)
Answer to Q37: a)
Answer to Q38: a)
JAVA OBEJECTIVE TYPE QUESTIONS
Q 1. What is the output of the following
StringBuffer sb1 = new StringBuffer("Amit");
StringBuffer sb2= new StringBuffer("Amit");
String ss1 = "Amit";
System.out.println(sb1==sb2);
System.out.println(sb1.equals(sb2));
System.out.println(sb1.equals(ss1));
System.out.println("Poddar".substring(3));
Ans:
a) false
false
false
dar
b) false
true
false
Poddar
c) Compiler Error
d) true
true
false
dar
Correct Answer is a)

***** Look carefully at code and answer the following
questions ( Q2 to Q8)
1 import java.applet.Applet;
2 import java.awt.*;
3 import java.awt.event.*;
4 public class hello4 extends Applet {
5 public void init(){
6 add(new myButton("BBB"));
7 }
8 public void paint(Graphics screen) {
9 }
10 class myButton extends Button{
11 myButton(String label){
12 super(label);
13 }
14 public String paramString(){
15 return super.paramString();
16 }
17 }
18 public static void main(String[] args){
19 Frame myFrame = new Frame(
20 "Copyright Amit");
21 myFrame.setSize(300,100);
22 Applet myApplet = new hello4();
23 Button b = new Button("My Button");
24 myApplet.add(b);
25 b.setLabel(b.getLabel()+"New");
26 // myButton b1 =(new hello4()).new
myButton("PARAMBUTTON");
27 System.out.println(b1.paramString());
28 myFrame.add(myApplet);
29 myFrame.setVisible(true);
30 myFrame.addWindowListener(new WindowAdapter(){
31 public void windowClosing(WindowEvent e){
32
System.exit(0);}});
33 }
34 } //End hello4 class.

Q2. If you run the above program via appletviewer (
defining a HTML file), You see on screen.
a) Two buttons
b) One button with label as "BBB"
c) One button with label as "My ButtonNew"
d) One button with label as "My Button"
Correct answer is b)

Q3. In the above code if line 26 is uncommented and
program runs as standalone application
a) Compile Error
b) Run time error
c) It will print the the label as PARAMBUTTON for button b1
Correct answer is c)

Q4 In the code if you compile as "javac hello4.java"
following files will be generated.
a) hello4.class, myButton.class,hello41.class
b)hello4.class, hello4$myButton.class,hello4$1.class
c)hello4.clas,hello4$myButton.class
Correct answer is b)

Q5. If above program is run as a standalone application.
How many buttons will be displayed
a) Two buttons
b) One button with label as "BBB"
c) One button with label as "My ButtonNew"
d) One button with label as "My Button"
correct answer is C)

Q6. If from line no 14 keyword "public" is removed, what
will happen.( Hint :paramString() method in
java.awt.Button is a protected method. (Assume line 26 is
uncommented)
a) Code will not compile.
b) Code will compile but will give a run time error.
c) Code will compile and no run time error.
Correct answer is a). As you can not override a method with
weaker access privileges

Q7. If from line no 14 keyword "public" is replaced with
"protected", what will happen.(Hint :paramString() method
in java.awt.Button is a protected method.(Assume line 26 is
uncommented)
a) Code will not compile.
b) Code will compile but will give a run time error.
c) Code will compile and no run time error.
Correct answer is c) . As you can access a protected
variable in the same package.

Q8.If line no 26 is replaced with Button b1 = new
Button("PARAMBUTTON").(Hint :paramString() method in
java.awt.Button is a protected method.(Assume line 26 is
uncommented)
a) Code will not compile.
b) Code will compile but will give a run time error.
c) Code will compile and no run time error.
Correct answer is a) Because protected variables and
methods can not be accssed in another package directly.
They can only be accessed if the class is subclassed and
instance of subclass is used.

Q9. What is the output of following if the return value is
"the value 0 if the argument string is equal to this
string; a value less than 0 if this string is
lexicographically less than the string argument; and a
value greater than 0 if this string is lexicographically
greater than the string argument" (Assuming written inside main)
String s5 = "AMIT";
String s6 = "amit";
System.out.println(s5.compareTo(s6));
System.out.println(s6.compareTo(s5));
System.out.println(s6.compareTo(s6));
Ans
a> -32
32
0
b> 32
32
0
c> 32
-32
0
d> 0
0
0
Correct Answer is a)

Q10) What is the output (Assuming written inside main)
String s1 = new String("amit");
String s2 = s1.replace('m','i');
s1.concat("Poddar");
System.out.println(s1);
System.out.println((s1+s2).charAt(5));
a) Compile error
b) amitPoddar
o
c) amitPoddar
i
d) amit
i
Correct answer is d)As String is imutable.so s1 is always
"amit". and s2 is "aiit".

Q11) What is the output (Assuming written inside main)
String s1 = new String("amit");
System.out.println(s1.replace('m','r'));
System.out.println(s1);
String s3="arit";
String s4="arit";
String s2 = s1.replace('m','r');
System.out.println(s2==s3);
System.out.println(s3==s4);
a) arit
amit
false
true
b) arit
arit
false
true
c) amit
amit
false
true
d) arit
amit
true
true
Correct answer is a) s3==s4 is true because java points both s3 and s4 to same memory
location in string pool

Q12) Which one does not extend java.lang.Number
1)Integer
2)Boolean
3)Character
4)Long
5)Short
Correct answer is 2) and 3)

Q13) Which one does not have a valueOf(String) method
1)Integer
2)Boolean
3)Character
4)Long
5)Short
Correct answer is 3)

Q.14) What is the output of following (Assuming written inside main)
String s1 = "Amit";
String s2 = "Amit";
String s3 = new String("abcd");
String s4 = new String("abcd");
System.out.println(s1.equals(s2));
System.out.println((s1==s2));
System.out.println(s3.equals(s4));
System.out.println((s3==s4));
a) true
true
true
false
b) true
true
true
true
c) true
false
true
false
Correct answer is a)

Q15. Which checkbox will be selected in the following code
( Assume with main and added to a Frame)
Frame myFrame = new Frame("Test");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox cb1 = new Checkbox("First",true,cbg);
Checkbox cb2 = new Checkbox("Scond",true,cbg);
Checkbox cb3 = new Checkbox("THird",false,cbg);
cbg.setSelectedCheckbox(cb3);
myFrame.add(cb1);
myFrame.add(cb2);
myFrame.add(cb3);
a) cb1
b) cb2,cb1
c) cb1,cb2,cb3
d) cb3
Correct Answer is d) As in a CheckboxGroup only one can be
selected

Q16) Which checkbox will be selected in the following code
( Assume with main and added to a Frame)
Frame myFrame = new Frame("Test");
CheckboxGroup cbg = new CheckboxGroup();
Checkbox cb1 = new Checkbox("First",true,cbg);
Checkbox cb2 = new Checkbox("Scond",true,cbg);
Checkbox cb3 = new Checkbox("THird",true,cbg);
myFrame.add(cb1);
myFrame.add(cb2);
myFrame.add(cb3);
a) cb1
b) cb2,cb1
c) cb1,cb2,cb3
d) cb3
Correct Answer is d) As in a CheckboxGroup only one can be
selected

Q17) What will be the output of line 5
1 Choice c1 = new Choice();
2 c1.add("First");
3 c1.addItem("Second");
4 c1.add("Third");
5 System.out.println(c1.getItemCount());
a) 1
b) 2
c) 3
d) None of the above
Correct Answer is c)

Q18) What will be the order of four items added
Choice c1 = new Choice();
c1.add("First");
c1.addItem("Second");
c1.add("Third");
c1.insert("Lastadded",2);
System.out.println(c1.getItemCount());
a) First,Second,Third,Fourth
b) First,Second,Lastadded,Third
c) Lastadded,First,Second,Third
Correct ANswer is b)

Q19) Answer based on following code
1 Choice c1 = new Choice();
2 c1.add("First");
3 c1.addItem("Second");
4 c1.add("Third");
5 c1.insert("Lastadded",1000);
6 System.out.println(c1.getItemCount());
a) Compile time error
b) Run time error at line 5
c) No error and line 6 will print 1000
d) No error and line 6 will print 4
Correct ANswer is d)

Q20) Which one of the following does not extends
java.awt.Component
a) CheckBox
b) Canvas
c) CheckbocGroup
d) Label
Correct answer is c)

Q21) What is default layout manager for panels and applets?
a) Flowlayout
b) Gridlayout
c) BorderLayout
Correct answer is a)

Q22) For awt components which of the following statements
are true?
a) If a component is not explicitly assigned a font, it
usese the same font that it container uses.
b) If a component is not explicitly assigned a foreground
color , it usese the same foreground color that it
container uses.
c) If a component is not explicitly assigned a backround
color , it usese the same background color that it
container uses.
d) If a component is not explicitly assigned a layout
manager , it usese the same layout manager that it
container uses.
correct answer is a),b),c)

Q23)java.awt.Component class method getLocation() returns
Point (containg x and y cordinate).What does this x and y
specify
a) Specify the postion of components lower-left component
in the coordinate space of the component's parent.
b) Specify the postion of components upper-left component
in the coordinate space of the component's parent.
c) Specify the postion of components upper-left component
in the coordinate space of the screen.
correct answer is b)

Q24. What will be the output of follwing
{
double d1 = -0.5d;
System.out.println("Ceil for d1 " + Math.ceil(d1));
System.out.println("Floor for d1 " +Math.floor(d1));
}
Answers:
a) Ceil for d1 0
Floor for d1 -1;
b) Ceil for d1 0
Floor for d1 -1.0;
c) Ceil for d1 0.0
Floor for d1 -1.0;
d) Ceil for d1 -0.0
Floor for d1 -1.0;
correct answer is d) as 0.0 is treated differently from -
0.0

Q25. What is the output of following
{
float f4 = -5.5f;
float f5 = 5.5f;
float f6 = -5.49f;
float f7 = 5.49f;
System.out.println("Round f4 is " + Math.round(f4));
System.out.println("Round f5 is " + Math.round(f5));
System.out.println("Round f6 is " + Math.round(f6));
System.out.println("Round f7 is " + Math.round(f7));
}
a)Round f4 is -6
Round f5 is 6
Round f6 is -5
Round f7 is 5
b)Round f4 is -5
Round f5 is 6
Round f6 is -5
Round f7 is 5
Correct answer is b)

Q26. Given Integer.MIN_VALUE = -2147483648
Integer.MAX_VALUE = 2147483647
What is the output of following
{
float f4 = Integer.MIN_VALUE;
float f5 = Integer.MAX_VALUE;
float f7 = -2147483655f;
System.out.println("Round f4 is " + Math.round(f4));
System.out.println("Round f5 is " + Math.round(f5));
System.out.println("Round f7 is " + Math.round(f7));
}
a)Round f4 is -2147483648
Round f5 is 2147483647
Round f7 is -2147483648
b)Round f4 is -2147483648
Round f5 is 2147483647
Round f7 is -2147483655
correct answer is a)
//Reason If the argument is negative infinity or any value
less than or equal to the value of Integer.MIN_VALUE, the
result is
equal to the value of Integer.MIN_VALUE.
If the argument is positive infinity or any value
greater than or equal to the value of Integer.MAX_VALUE,
the result is
equal to the value of Integer.MAX_VALUE. // From JDK api
documentation

Q27)
1 Boolean b1 = new Boolean("TRUE");
2 Boolean b2 = new Boolean("true");
3 Boolean b3 = new Boolean("JUNK");
4 System.out.println("" + b1 + b2 + b3);
a) Comiler error
b) RunTime error
c)truetruefalse
d)truetruetrue
Correct answer is c)

Q28) In the above question if line 4 is changed to
System.out.println(b1+b2+b3); The output is
a) Compile time error
b) Run time error
c) truetruefalse
d) truetruetrue
Correct answer is a) As there is no method to support
Boolean + Boolean
Boolean b1 = new Boolean("TRUE");
Think ----->System.out.println(b1); // Is this valid or
not?

Q29. What is the output
{
Float f1 = new Float("4.4e99f");
Float f2 = new Float("-4.4e99f");
Double d1 = new Double("4.4e99");
System.out.println(f1);
System.out.println(f2);
System.out.println(d1);
}
a) Runtime error
b) Infinity
-Infinity
4.4E99
c) Infinity
-Infinity
Infinity
d) 4.4E99
-4.4E99
4.4E99
Correct answer is b)

Q30 Q. Which of the following wrapper classes can not
take a "String" in constructor
1) Boolean
2) Integer
3) Long
4) Character
5) Byte
6) Short
correct answer is 4)

Q31. What is the output of following
Double d2 = new Double("-5.5");
Double d3 = new Double("-5.5");
System.out.println(d2==d3);
System.out.println(d2.equals(d3));
a) true
true
b) false
false
c) true
false
d) false
true
Correct answer is d)

Q32) Which one of the following always honors the
components's preferred size.
a) FlowLayout
b) GridLayout
c) BorderLayout
Correct answer is a)

Q33) Look at the following code
import java.awt.*;
public class visual extends java.applet.Applet{
static Button b = new Button("TEST");
public void init(){
add(b);
}
public static void main(String args[]){
Frame f = new Frame("Visual");
f.setSize(300,300);
f.add(b);
f.setVisible(true);
}
}
What will happen if above code is run as a standalone
application
a) Displays an empty frame
b) Displays a frame with a button covering the entire frame
c) Displays a frame with a button large enough to
accomodate its label.
Correct answer is b) Reason- Frame uses Border Layout which
places the button to CENTRE
(By default) and ignores Button's preferred size.

Q34 If the code in Q33 is compiled and run via
appletviewer what will happen
a) Displays an empty applet
b) Displays a applet with a button covering the entire
frame
c) Displays a applet with a button large enough to
accomodate its label.
Correct answer is c) Reason- Applet uses FlowLayout which
honors Button's preferred size.

Q35. What is the output
public static void main(String args[]){
Frame f = new Frame("Visual");
f.setSize(300,300);
f.setVisible(true);
Point p = f.getLocation();
System.out.println("x is " + p.x);
System.out.println("y is " + p.y);
}
a) x is 300
y is 300
b) x is 0
y is 0
c) x is 0
y is 300
correct answer is b) Because postion is always relative to
parent container and in this
case Frame f is the topemost container

Q36) Which one of the following always ignores the
components's preferred size.
a) FlowLayout
b) GridLayout
c) BorderLayout
Correct answer is b)

Q37) Consider a directory structure like this (NT or 95)
C:\JAVA\12345.msg --FILE
\dir1\IO.class -- IO.class is under dir1
Consider the following code
import java.io.*;
public class IO {
public static void main(String args[]) {
File f = new File("..\\12345.msg");
try{
System.out.println(f.getCanonicalPath());
System.out.println(f.getAbsolutePath());
}catch(IOException e){
System.out.println(e);
}
}
}

What will be the output of running "java IO" from
C:\java\dir1
a) C:\java\12345.msg
C:\java\dir1\..\12345.msg
b) C:\java\dir1\12345.msg
C:\java\dir1\..\12345.msg
c) C:\java\dir1\..\12345.msg
C:\java\dir1\..\12345.msg
correct answer is a) as getCanonicalPath Returns the
canonical form of this File object's pathname. The precise
definition of canonical form is system-dependent, but it
usually
specifies an absolute pathname in which all relative
references and references to the current user directory
have been completely resolved.
WHERE AS
getAbsolutePath Returns the absolute pathname of the file
represented by this object. If this object represents an
absolute pathname, then return the pathname. Otherwise,
return a pathname that is a concatenation of the current
user directory, the separator character, and the pathname
of this file object.

Q38) Suppose we copy IO.class from C:\java\dir1 to c:\java
What will be the output of running "java IO" from
C:\java.
a) C:\java\12345.msg
C:\java\..\12345.msg
b) C:\12345.msg
C:\java\..\12345.msg
c) C:\java\..\12345.msg
C:\java\\..\12345.msg
correct answer is b)

Q39) Which one of the following methods of java.io.File
throws IOException and why
a) getCanonicalPath and getAbsolutePath both require
filesystem queries.
b) Only getCannonicalPath as it require filesystem queries.
c) Only getAbsolutePath as it require filesystem queries.
Correct answer is b)

Q40) What will be the output if
Consider a directory structure like this (NT or 95)
C:\JAVA\12345.msg --FILE
\dir1\IO.class -- IO.class is under dir1
import java.io.*;
public class IO {
public static void main(String args[]) {
File f = new File("12345.msg");
String arr[] = f.list();
System.out.println(arr.length);
}
}
a) Compiler error as 12345.msg is a file not a directory
b) java.lang.NullPointerException at run time
c) No error , but nothing will be printed on screen
Correct ansewer is b)

Q41) What will be the output
Consider a directory structure like this (NT or 95)
C:\JAVA\12345.msg --FILE
import java.io.*;
public class IO {
public static void main(String args[]) {
File f1 = new File("\\12345.msg");
System.out.println(f1.getPath());
System.out.println(f1.getParent());
System.out.println(f1.isAbsolute());
System.out.println(f1.getName());
System.out.println(f1.exists());
System.out.println(f1.isFile());
}
}
a) \12345.msg
\
true
12345.msg
true
true
b) \12345.msg
\
true
\12345.msg
false
false
c) 12345.msg
\
true
12345.msg
false
false
d) \12345.msg
\
true
12345.msg
false
false
correct answer is d)

Q42) If in question no 41 the line
File f1 = new File("\\12345.msg"); is replaced with
File f1 = new File("12345.msg");
What will be the output
a) 12345.msg
\
true
12345.msg
true
true
b) 12345.msg
null
true
12345.msg
true
true
c) 12345.msg
null
false
12345.msg
true
true
d) \12345.msg
\
true
12345.msg
false
false
Correct answer is c)

Comments

Popular posts from this blog

MCA-3 COMPUTER GRAPHICS

MCA-5 Data warehouse & Data Mining

BCA-1, COMMUNICATION SKILLS IN ENGLISH