Java MCQ
Java MCQ
2. The method length( ) will return an integer which represents the length of a string value. [a] true [b] false
3. The name of a variable is known as its: [a] identifier [b] constant [c] data type [d] base
. !ll variables must be declared before they can be used. [a] true [b] false
". !ll strings end with a null #ero inside memory. [a] true [b] false
$. %ariable names may begin with a number. [a] true [b] false
-. ,n .ava/ "02 1 [a] 2 [b] 2.5 [c] 3 [d] none of the above
2. ,n .ava/ the 3 refers to [a] percentages [b] modulus operator [c] division [d] data storage
14. The method toLowerCase( ) changes all lowercase letters to uppercase and all uppercase letters to lowercase. [a] true [b] false
11. %ariables that describe the data stored at that particular memory location are called [a] identifiers [b] constant variables [c] floating point variables [d] mnemonic variables
13. The following statement is valid double price = 7, !".#$; [a] true [b] false
1". The method inde%&'(",") will allow you to search for the location of a comma. [a] true [b] false
1$. 6athematicians and computers interpret the e(ual sign 718 in the same way. [a] true [b] false
1&. ! character variable may contain up to seven letters. [a] true [b] false
1-. 9hen a data type must contain decimal numbers/ assign the type [a] int [b] char [c] double [d] long int
12. %ariables that are declared/ but not initiali#ed/ contain [a] blank spaces [b] eros [c] :garbage: values [d] nothing ! they are e"pty
24. ! variable is given a value through [a] os"osis [b] as assignment statement [c] the i"port state"ents [d] the public class declarations
#hat is the si e of a double variable in $ava% a. b. c. d. 2 bytes 4 bytes - bytes &t depends on the co"piler setting
a.
b. c. d.
+ 15 123
3.
e. ,lass,ast-(ception #hich of the follo)ing best describes the set of all pairs of values for boolean variables a and b. such that
(!a && b) !(a !! b)
#hen you try to co"pile $y%lass. the 5ava co"piler gives an error "essage 4.
2134
$y%lass is not abstra&t and does not o'erride abstra&t met(od )some met(od* in +a'a.util.%omparator
#hich of the follo)ing could replace )missing statement* so that -est2001 )ould co"pile )ith no errors and
-est2001 test test.print(); new -est2001();
)ould display 2001% &. System.out.println(new /ear2001()); &&. System.out.println(new -est2001()); &&&. System.out.println(t(is); a. b. c. d. e. & only && only & and && && and &&& ,/ ,,/ and ,,,
a. b. c. d.
0 1 2 3
list.add("6");
;.
10.
#hat is the "ini"u" nu"ber of "ethods that "ust be defined in &lassC for it to co"pile )ith no errors% a. b. c. d. e. <o particular "ethods are re=uired
met(od@ met(odC met(od@ met(od@
11.
c. d.
e. %lass@ ob+ new %lassC(); ?uestions 12@13 refer to the following method rotate that takes a two@dimensional array a and retu
publi& int5656 rotate(int5656a) 2 int rows )e4pression 1*; int &ols )e4pression 2*; int5656 b )e4pression 3*; #or (int r 0; r ) rows; r++) #or (int & 0; & ) &ols; &++) b5r65&6 )e4pression 7*; return b; 3
)ill hold
12. #hich of the follo)ing should replace )e4pression 1*. )e4pression 2*. and )e4pression 3*%
2134
?e(pression 1@ a. b. c. d. e.
a.&ols a.&ols a.&ols()
?e(pression 2@
a.rows a.rows a.rows() a.lengt( a.lengt(
?e(pression 3@
new int(rows" &ols) new int5656(rows" &ols) new int(rows" &ols) new int5rows65&ols6 new int5656(rows" &ols)
a506.lengt( a506.lengt(
a.
a5&65r6
b. c. d.
e. a5rows 9 1 9 r65&ols 9 1 9 &6 #hat is the value of n after the follo)ing code is e(ecuted% 14.
int n 2001; #or (int i 0; i ) 10; i++) n (n + 3) E 2;
a. b. c. d.
0 1 2 3
a. b. c. d. e. 1+.
5@tlanta" Coston6 5@tlanta" +" Coston6 5@tlanta" Coston" +6 5@tlanta" +" Coston" +6
#hich of the follo)ing state"ents is true% a. b. c. 1 static variable cannot be initiali ed in a constructor 1 static variable "ust be declared final 1n instance variable can*t be declared final
d. e.
2134
1 static "ethod can*t access an instance variable /nly a static "ethod can access a static variable
a and b are arrays of integers and each of the" has n ele"ents. a is sorted in ascending order and b is sort 1A. co"parisons in an opti"al algorith" that deter"ines )hether there e(ists i. such that a5i6 b5i6%
a. b. c. d.
?ey ""; $ap map new -ree$ap(); #or (int ? 0; ? ) 3; ?++) 2 ?ey + ?; String 'alue "@"; map.put(?ey" 'alue); 'alue + "C"; map.putG?ey" 'alue); 3 System.out.println(map.siFe());
a. b. c. d.
1 2 3 4
i# (root null !! (root.get<e#t() null && root.get>ig(t() return null; else 2 root.set<e#t(mysteryHro&ess(root.get<e#t())); root.set>ig(t(mysteryHro&ess(root.get>ig(t())); return root; 3
b.
1 I
c.
3 2
1 E I
Sta&? st? new @rraySta&?(); st?.pus(("@"); st?.pus(("C"); st?.pus((st?); w(ile(!st?.is;mpty()) 2 .b+e&t ob+ st?.pop(); i# (ob+ instan&e.# Sta&?) 2 w(ile(!((Sta&?)ob+).is;mpty()) 2 System.out.print(((Sta&?)ob+).pop()); 3 else System.out.print(ob+); 3
a. b. c. d. e.
,lass,ast-(ception
1 priority =ueue is represented as a "ini"u" heap. stored in an array. #hen a node 21. is added to the heap. it is first added at the left"ost vacant spot in the current level 2134 2or. if the current level is full. at the left"ost spot in the ne(t level4. and then the reheap!up procedure is applied. #hat is the order of the values in the array after B$B.
B@B. BAB. BJB. B.B. B;B. BSB are added to the =ueue% a. b. c. d. e.
@;J$A.S @J;$.AS @;J$A.S @;JS.$A @J;SA.$
Cuppose an array of n ele"ents is stored in ascending order. 'hen 5 ele"ents are 22. picked rando"ly and assigned rando" values. #hich of the follo)ing sorting 2134 algorith"s guarantees to restore the ascending order in than array using no "ore than O2n4 co"parisons% &. Celection Cort a. b. c. d. e. &&. &nsertion Cort & only && only & and && && and &&& &. &&. and &&& &&&. Dergesort