The document discusses questions about basics of the Java programming language including operators, variables, declarations, and control structures. It provides true/false questions and explanations about concepts like modulus, declarations, bitwise operations, if/else statements, and loops. Key points covered include the valid ranges and defaults for variable types, precedence of operations, evaluating boolean and binary expressions, and the proper usage of control flow statements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
64 views9 pages
Sample File
The document discusses questions about basics of the Java programming language including operators, variables, declarations, and control structures. It provides true/false questions and explanations about concepts like modulus, declarations, bitwise operations, if/else statements, and loops. Key points covered include the valid ranges and defaults for variable types, precedence of operations, evaluating boolean and binary expressions, and the proper usage of control flow statements.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9
Answer the following questions about basics of the Java programming language.
These include concepts like operators, variables, declarations, basic language
structures such as if, for, switch, etc. 1. The modulus operator (%) in Java can be used onl with variables of integer tpe. ! True ! "alse Answer: "alse The modulus operator (%) ma be used with floating#point as well as integer tpes. $t returns the remainder of a division operation, e.g., %& % ' will return (. 2. )eclarations must appear at the start of the bod of a Java method. ! True ! "alse Answer: "alse The can appear anwhere within the bod of the method. 3. All bitwise operations are carried out with the same level of precedence in Java. ! True ! "alse Answer: "alse All operations in Java, including the bitwise operations, are carried out with a definite precedence. 4. The operations ** + and *** + produce the same result when * &. ! True ! "alse Answer: True The shift operation ,% *** -, is identical to ,% ** -, for all positive values of %. $t shifts the bits in % to the right b - positions. 5. consider the statement ,. / (a * b) 0 a 1 b,2 then the value of . is -3, if a / %4 and b / -3. ! True ! "alse Answer: True the statement is equivalent to1 if (a * b) . / a2 else . / b2 6. 5henever the ,66, operator is used, such as in1 exp1 && exp2 where e.p% and e.p- are boolean e.pressions, both the boolean e.pressions are not alwas evaluated. ! True ! "alse Answer: True $f the first e.pression is false, the result of the ,66, operation will alwas be false regardless of the second e.pression. The ,6, operator on the other hand forces the evaluation of the second e.pression even if the first e.pression is false. 7. The e.pression ( */ 7 66 a // b) is evaluated b first evaluating the e.pression */ 7, and then evaluating a // b. ! True ! "alse Answer: "alse $f */ 7 is false, then there is no need to evaluate the second e.pression. 8. The ,switch, selection structure must end with the default case. ! True ! "alse Answer: "alse The default case in a switch structure is optional and onl used when none of the other cases match. 9. "or the e.pression ( */ 7 66 a // b) to be true, at least one of ( */ 7) and (a // b) must be true. ! True ! "alse Answer: "alse "or the result of a ,66, operation to be true, both operands must be true. 10. A break statement must alwas be present in the default case of a ,switch, selection structure. ! True ! "alse Answer: "alse The break statement, which brings the computation out of the ,switch, selection structure, is not required for the defalut case. 11. 8ariables declared inside a for loop are limited in scope to the loop. ! True ! "alse Answer: "alse An variable declared within a block statement such as a for or if cannot be referenced outside the block. 12. An arra in the Java programming language has the abilit to store man different tpes of values. ! True ! "alse Answer: "alse All elements of an erra must be of the same tpe. 9owever, it is possible to declare an arra of ob:ects, which ma of instances of different classes. 13. An individual arra element from an arra of tpe int, when passed to a method is passed b value. ! True ! "alse Answer: True $ndividual elements of an arra of this tpe are passed b value as a parameter to a method. An changes to them in the method will not change the value of the arra element. 14. ;b:ects of a super class can alwas be assigned to a subclass reference. ! True ! "alse Answer: "alse ;b:ects of a subclass ma be assigned to a super class reference. "ood for thought1 is there a loss in functionalit when this is done0 15. ;b:ects of a subclass can be assigned to a super class reference. ! True ! "alse Answer: True ;b:ects of a super class ma not be assigned to a sub class reference. "ood for thought1 wh is it so0 16. The // operator can be used to compare two String ob:ects. The result is alwas true if the two strings are identical. ! True ! "alse Answer: "alse <tring ob:ects must be compared using the ,equals, method of one of the ob:ects. "ood for thought1 will the // operator ever return true when two string ob:ects are compared using it0 4. What is wrong with the following code? switch (x) { case 1: n1 = 10; n2 = 20; case 2: n3 = 30; break; n = 0; ! Answer: n = 0; is "nreachable What does the following frag#ent dis$la%? &tring s = 'six:' ( 3 ( 3; &%ste#)o"t)$rintln(s); Answer1 six:33 Answer the following questions about basic operations in the Java programming language. 1. The tpe long can be used to store values in the following range1 ! #-+% to -+% # % ! #-'( to -'( ! #-+- to -+- # % ! #-'+ to -'+ # % Answer: #-'+ to -'+ # % The tpe long is a '(#bit two=s complement that can be used for long integers. 2. 5hich of the following is not a he.adecimal number0 (A) >>> (?) (he.)-+ (@) &.AA' ()) &.%"- ! (A) ! (A) 6 (?) ! (@) ! (A), (?) 6 (@) Answer: (A) 6 (?) 9e.adecimal numbers start with ,&., in the Java programming language. 3. <elect the invalid assignment statements from the following1 (A) float . / -+4.442 (?) double / &.((+2 ! (?) ! (?) 6 ()) ! (A) 6 (?) ! (A) 6 (@) Answer: (A) 6 (@) -+4.44 is a double in Java. To assign this value to a float, -+4.44f must be used. A variable of tpe boolean cannot be converted to an integer in Java. 4. $f not assigned a value, a variable of tpe char has the following default value1 ! =Buffff= ! , , (space) ! =Bu&&&&= ! =Bu&&&%= Answer: =Bu&&&&= The value of a variable of tpe char is %' bits of data formatted as a Cnicode character. 5. %A 6 -> / 0 ! (( ! %( ! %+ ! %- Answer: %+ 5hen written in binar format, %A is %%%%, and -> is %%%&%. %%%% 6 %%%&% is %%&%, which is %+ in decimal. 6. -3 D 4 / 0 ! -3 ! 4 ! %> ! +A Answer: -3 5hen written in binar format, -3 is %%&%%, and 4 is %&&&. %%&%% D %&&& is %%&%%, which is -3. 7. $dentif the statements that are correct1 (A) int a / %+, a**- / + (?) int b / #4, b**% / #( (@) int a / %+, a***- / + ()) int b / #4, b***% / #( ! (A), (?), (@) 6 ()) ! (A) 6 (?) ! (@) 6 ()) ! (A), (?) 6 (@) Answer: (A), (?) 6 (@) The shift operation ,% *** -, is identical to ,% ** -, for all positive values of %. $t shifts the bits in % to the right b - positions. 8. @onsider the following code1 int x* %* +; % = 1; + = ,; x = 0 - (((%) ( +((; After e.ecution of this, what will be the values of ., and 70 ! x = * % = 1* + = , ! x = 3* % = 2* + = . ! x = -/* % = 1* + = , ! x = * % = 2* + = . Answer: x = 3* % = 2* + = . ?oth and 7 get incremented as a result of the last statement, but gets incremented before and 7 after the assisgment of the calculated value to .. 9. 5hat will be the result of the e.pression a % b when a and b are of tpe int and their values are a / %& and b / '0 ! %.'' ! % ! Eone of these ! ( Answer: ( The modulus operator (%) ma be used with floating#point as well as integer tpes. $t returns the remainder of a division operation. Therefore, %& % ' will return (. 10. 5hat will be the result of the e.pression a % b when a and b are of tpe int and their values are a / #%3 and b / #'0 ! #A ! A ! #-+ ! Eone of these Answer: A a % b calculates the remainder when a is divided b b 11. 5hat will be the value of a after e.ecution of the following statements1 int a = 23* b = 3; a = ((a 0 b) ? (b ( a) : (b - a); ! A3 ! %% ! -+ ! +( ! 1rror) 2annot be exec"ted. Answer: A3 12. 2hoose the o$erations that can be $erfor#ed on &tring ob3ects: (4) ( (5) ( = (2) - (6) 7 (1) 8 ! (4) ! (6)9o$tion: ! (6) ; (1) ! <one of these ! (4) ; (5) Answer: (4) ; (5) =he '(' and '( =' o$erators are o>erlaoded in ?a>a) =he% 3oin the two o$erands into one &tring ob3ect) 13. (1 @ ) ( ( ; 2) = ? (in base ten) ! % ! - ! 4 ! + ! A Answer: A An binar%* 1 @ is 1 @ 100 = 101* which is , in deci#al) &i#ilarl%* ; 2 is 100 ; 10 = 0* which is 0 in deci#al) 14. Bf the following f"nctions fro# the class 3a>a)lang)Cath* select those that $rod"ce a >al"e of 2,* when a = 2,)/. ! (2) ! (6) ; (1) ! (4) ! (2) ; (1) ! (4)* (2) ; (1) Answer: (4)* (2) ; (1) =he f"ntions ceil* rint and ro"nd all ret"rn the >al"e 2, in this case) =he difference lies in the t%$e of arg"#ent ret"rned: ceil and rint ret"rn a do"ble* where as ro"nd will ret"rn an int or a long* de$ending "$on the t%$e of a (float or do"ble* res$ecti>el%)) 15. An the code below* what data t%$es the >ariable x can ha>e? b%te b1 = ,; b%te b2 = 10; x = b1 D b2;(4) b%te (5) int (2) short (6) long (1) float (E) do"ble ! (4)* (5)* (6) ; (1) ! (5)* (2) ; (6) ! (5)* (6) ; (1) ! (6) ; (E) ! (5)* (6)* (1) ; (E) Answer: (5)* (6)* (1) ; (E) 16. Fi>en the declarations int x * # = 2000; short %; b%te b1 = -0* b2; long n; Which of the following assign#ent state#ents will e>al"ate correctl%? (4) x = # D b1; (5) % = # D b1; (2) n = # D 3G; (6) x = # D 3G; ! (4)* (5) ; (2) ! (4)* (2) ; (6) ! (5) ; (2) ! (4) ; (2) ! (4) ; (6) Answer: (4) ; (2) 17. Fi>en the declarations boolean b; short x1 = 100* x2 = 200* x3 = 300; Which of the following state#ents are e>al"ated to tr"e? (4) b = x1 D 2 == x2; (5) b = x1 ( x2 H= 3 D x1; (2) b = (x3 - 2Dx200) @@ ((x3 = 00)02DDx2); (6) b = (x3 - 2Dx2:0) @@ ((x3 = 00) 2Dx2); ! (4)* (5) ; (2) ! (4)* (2) ; (6) ! (5) ; (2) ! (4) ; (6) ! (4) ; (2) Answer: (4) ; (2) 18. An which of the following code frag#ents* is the >ariable x e>al"ated to I? (4) int x = 32; x = x::2; (5) int x = 33; x = x::2; (2) int x = 3,; x = x::2; (6) int x = 1.; x = x::1; ! (4)* (5) ; (2) ! (4)* (2) ; (6) ! (5) ; (2) ! (4)* (5)* (2) ; (6) ! (4) ; (6) Answer: (4)* (5)* (2) ; (6) 19. 2onsider the following code sni$$et: ))))) ))))) tr% ! 1rror) WonJt co#$ile ! 6i>ision b% +ero ! 6i>ision b% +ero 2atch block ! 2atch block Answer: 2atch block 20. Which of the following re$resent legal flow control state#ents? (4) break; (5) break(); (2) contin"e o"ter; (6) contin"e(inner); (1) ret"rn; (E) exit(); ! (4)* (5) ; (2) ! (4)* (2) ; (6) ! (4)* (2) ; (1) ! (4) ; (1) ! (2) ; (1) Answer: (4)* (2) ; (1)