0% found this document useful (0 votes)
206 views13 pages

New Text Document

This document contains 23 multiple choice questions related to Java applets, AWT components, strings, and other Java concepts. Each question is followed by 4 possible answers and the correct answer. The questions cover topics like StringBuffer, applet lifecycle, button labels, layout managers, and more.

Uploaded by

api-3719535
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
206 views13 pages

New Text Document

This document contains 23 multiple choice questions related to Java applets, AWT components, strings, and other Java concepts. Each question is followed by 4 possible answers and the correct answer. The questions cover topics like StringBuffer, applet lifecycle, button labels, layout managers, and more.

Uploaded by

api-3719535
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 13

original to be found at http://members.theglobe.com/apoddar/questions.

html
saved from :
http://www.geocities.com/siliconvalley/orchard/9362/java/javacert/poddar.html

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)

q 10) 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".

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

q 28) 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?

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

q 38) 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)

You might also like