Answer & ExplanationAnswer: Option CExplanation:Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing.Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be "A && B". Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. Option D is wrong. The output is "notB". |
2. | switch(x) { default: System.out.println("Hello"); }Which two are acceptable types for x? 1. byte 2. long 3. char 4. float 5. Short 6. Long | | A. | 1 and 3 | B. | 2 and 4 | C. | 3 and 5 | D. | 4 and 6 |
Answer & ExplanationAnswer: Option AExplanation:Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not be used as variables. |
3. | public void test(int x) { int odd = 1; if(odd) /* Line 4 */ { System.out.println("odd"); } else { System.out.println("even"); } }Which statement is true? | | A. | Compilation fails. | B. | "odd" will always be output. | C. | "even" will always be output. | D. | "odd" will be output for odd values of x, and "even" for even values. |
Answer & ExplanationAnswer: Option AExplanation:The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer. |
4. | public class While { public void loop() { int x= 0; while ( 1 ) /* Line 6 */ { System.out.print("x plus one is " + (x + 1)); /* Line 8 */ } } }Which statement is true? | | A. | There is a syntax error on line 1. | B. | There are syntax errors on lines 1 and 6. | C. | There are syntax errors on lines 1, 6, and 8. | D. | There is a syntax error on line 6. |
Answer & ExplanationAnswer: Option DExplanation:Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java.A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown. |
(5)Which three form part of correct array declarations? 1. public int a [ ] 2. static int [ ] a 3. public [ ] int a 4. private int a [3] 5. private int [3] a [ ] 6. public final int [ ] a A. | 1, 3, 4 | B. | 2, 4, 5 | C. | 1, 2, 6 | D. | 2, 5, 6 |
Answer & Explanation
Answer: Option C
Explanation:
(1), (2) and (6) are valid array declarations.
Option (3) is not a correct array declaration. The compiler complains with: illegal start of type. The brackets are in the wrong place. The following would work: public int[ ] a
Option (4) is not a correct array declaration. The compiler complains with: ']' expected. A closing bracket is expected in place of the 3. The following works: private int a []
Option (5) is not a correct array declaration. The compiler complains with 2 errors:
']' expected. A closing bracket is expected in place of the 3 and expected A variable name is expected after a[ ]
(6)What will be the output of the program? class Equals { public static void main(String [] args) { int x = 100; double y = 100.1; boolean b = (x = y); /* Line 7 */ System.out.println(b); } } A. | true | B. | false | C. | Compilation fails | D. | An exception is thrown at runtime |
Answer & Explanation
Answer: Option C
Explanation:
The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.
Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.
(7)What will be the output of the program? class PassS { public static void main(String [] args) { PassS p = new PassS(); p.start(); } void start() { String s1 = "slip"; String s2 = fix(s1); System.out.println(s1 + " " + s2); } String fix(String s1) { s1 = s1 + "stream"; System.out.print(s1 + " "); return "stream"; } } A. | slip stream | B. | slipstream stream | C. | stream slip stream | D. | slipstream slip stream |
Answer & Explanation
Answer: Option D
Explanation:
When the fix() method is first entered, start()'s s1 and fix()'s s1 reference variables both refer to the same String object (with a value of "slip"). Fix()'s s1 is reassigned to a new object that is created when the concatenation occurs (this second String object has a value of "slipstream"). When the program returns to start(), another String object is created, referred to by s2 and with a value of "stream".
(8)public class Test { }
What is the prototype of the default constructor? A. | Test( ) | B. | Test(void) | C. | public Test( ) | D. | public Test(void) |
Answer & Explanation
Answer: Option C
Explanation:
Option A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class).
Option D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out.
(9)Which four options describe the correct default values for array elements of the types indicated? 1. int -> 0 2. String -> "null" 3. Dog -> null 4. char -> '\u0000' 5. float -> 0.0f 6. boolean -> true A. | 1, 2, 3, 4 | B. | 1, 3, 4, 5 | C. | 2, 4, 5, 6 | D. | 3, 4, 5, 6 |
Answer & Explanation
Answer: Option B
Explanation:
(1), (3), (4), (5) are the correct statements.
(2) is wrong because the default value for a String (and any other object reference) is null, with no quotes.
(6) is wrong because the default value for boolean elements is false.
10. What is difference between Path and Classpath?
Path and Classpath are operating system level environment variales. Path is used define where the system can find the executables(.exe) files and classpath is used to specify the location .class files.
6. | (11)What will be the output of the program? public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 3; z++) { switch (z) { case y: System.out.print("0 "); /* Line 11 */ case x-1: System.out.print("1 "); /* Line 12 */ case x: System.out.print("2 "); /* Line 13 */ } } } } | | A. | 0 1 2 | B. | 0 1 2 1 2 2 | C. | Compilation fails at line 11. | D. | Compilation fails at line 12. |
Answer & ExplanationAnswer: Option CExplanation:Case expressions must be constant expressions. Since x is marked final, lines 12 and 13 are legal; however y is not a final so the compiler will fail at line 11. |
(12) | What will be the output of the program? public class If1 { static boolean b; public static void main(String [] args) { short hand = 42; if ( hand < 50 & !b ) /* Line 7 */ hand++; if ( hand > 50 ); /* Line 9 */ else if ( hand > 40 ) { hand += 7; hand++; } else --hand; System.out.println(hand); } } | | A. | 41 | B. | 42 | C. | 50 | D. | 51 |
Answer & ExplanationAnswer: Option DExplanation:In Java, boolean instance variables are initialized to false, so the if test on line 7 is true and hand is incremented. Line 9 is legal syntax, a do nothing statement. The else-if is true so hand has 7 added to it and is then incremented.View Answer Workspace Report Discuss in Forum |
(13 | )What will be the output of the program? public class Test { public static void main(String [] args) { int I = 1; do while ( I < 1 ) System.out.print("I is " + I); while ( I > 1 ) ; } } | | A. | I is 1 | B. | I is 1 I is 1 | C. | No output is produced. | D. | Compilation error |
Answer & ExplanationAnswer: Option CExplanation:There are two different looping constructs in this problem. The first is a do-while loop and the second is a while loop, nested inside the do-while. The body of the do-while is only a single statement-brackets are not needed. You are assured that the while expression will be evaluated at least once, followed by an evaluation of the do-while expression. Both expressions are false and no output is produced. |
(14) | What will be the output of the program? int x = l, y = 6; while (y--) { x++; } System.out.println("x = " + x +" y = " + y); | | A. | x = 6 y = 0 | B. | x = 7 y = 0 | C. | x = 6 y = -1 | D. | Compilation fails. |
Answer & ExplanationAnswer: Option DExplanation:Compilation fails because the while loop demands a boolean argument for it's looping condition, but in the code, it's given an int argument.while(true) { //insert code here } | (15) What will be the output of the program? class Test { public static void main(String [] args) { int x=20; String sup = (x < 15) ? "small" : (x < 22)? "tiny" : "huge"; System.out.println(sup); } } | A. | small | B. | tiny | C. | huge | D. | Compilation fails |
Answer & ExplanationAnswer: Option BExplanation:This is an example of a nested ternary operator. The second evaluation (x < 22) is true, so the "tiny" value is assigned to sup. |
| (16) What will be the output of the program? class Test { public static void main(String [] args) { int x= 0; int y= 0; for (int z = 0; z < 5; z++) { if (( ++x > 2 ) && (++y > 2)) { x++; } } System.out.println(x + " " + y); } } | | A. | 5 2 | B. | 5 3 | C. | 6 3 | D. | 6 4 |
Answer & ExplanationAnswer: Option CExplanation:In the first two iterations x is incremented once and y is not because of the short circuit && operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration x is doubly incremented and y is incremented. |
(17). | What will be the output of the program? class Test { public static void main(String [] args) { int x= 0; int y= 0; for (int z = 0; z < 5; z++) { if (( ++x > 2 ) || (++y > 2)) { x++; } } System.out.println(x + " " + y); } } | | A. | 5 3 | B. | 8 2 | C. | 8 3 | D. | 8 5 |
Answer & ExplanationAnswer: Option BExplanation:The first two iterations of the for loop both x and y are incremented. On the third iteration x is incremented, and for the first time becomes greater than 2. The short circuit or operator || keeps y from ever being incremented again and x is incremented twice on each of the last three iterations. |
(18) | What will be the output of the program? class Bitwise { public static void main(String [] args) { int x = 11 & 9; int y = x ^ 3; System.out.println( y | 12 ); } } | | A. | 0 | B. | 7 | C. | 8 | D. | 14 |
Answer & ExplanationAnswer: Option DExplanation:The & operator produces a 1 bit when both bits are 1. The result of the & operation is 9. The ^ operator produces a 1 bit when exactly one bit is 1; the result of this operation is 10. The | operator produces a 1 bit when at least one bit is 1; the result of this operation is 14.View Answer Workspace Report Discuss in Forum |
(19). | What will be the output of the program? class SSBool { public static void main(String [] args) { boolean b1 = true; boolean b2 = false; boolean b3 = true; if ( b1 & b2 | b2 & b3 | b2 ) /* Line 8 */ System.out.print("ok "); if ( b1 & b2 | b2 & b3 | b2 | b1 ) /*Line 10*/ System.out.println("dokey"); } } | | A. | ok | B. | dokey | C. | ok dokey | D. | No output is produced | E. | Compilation error |
Answer & ExplanationAnswer: Option BExplanation:The & operator has a higher precedence than the | operator so that on line 8 b1 and b2 are evaluated together as are b2 & b3. The final b1 in line 10 is what causes that if test to be true. Hence it prints "dokey". | (20) What will be the output of the program? class Test { static int s; public static void main(String [] args) { Test p = new Test(); p.start(); System.out.println(s); } void start() { int x = 7; twice(x); System.out.print(x + " "); } void twice(int x) { x = x*2; s = x; } } A. | 7 7 | B. | 7 14 | C. | 14 0 | D. | 14 14 |
Answer & Explanation
Answer: Option B
Explanation:
The int x in the twice() method is not the same int x as in the start() method. Start()'s x is not affected by the twice() method. The instance variable s is updated by twice()'s x, which is 14. (21) What will be the output of the program? public class Test { public static void leftshift(int i, int j) { i
You May Also Find These Documents Helpful
-
6 . For an unconditional approach to a particular part of a complex PL/SQL block, which of the following control structures can be used?…
- 1980 Words
- 8 Pages
Good Essays -
1. A while loop will not execute the body of the code if you have your test condition incorrect.…
- 3740 Words
- 15 Pages
Satisfactory Essays -
2. What type of instruction is used to execute different code based on a condition? -Jumps…
- 239 Words
- 2 Pages
Satisfactory Essays -
4. (TCO D) Which of the following is not an example of a logical operator? (Points : 5)…
- 1919 Words
- 8 Pages
Powerful Essays -
1) Numbers can be represented in binary (using 2 symbols 0 and 1) and hexadecimal ( 16 symbols…
- 480 Words
- 2 Pages
Satisfactory Essays -
* Provide a set of values that will test the normal operation of this program segment. Defend your choices.…
- 1775 Words
- 8 Pages
Powerful Essays -
Each character requires one byte as it is usually stored as an ASCII character. Notice that a digit such as 8 could be held as either a character, an integer or even a real. If any calculations are going to take place on the value then it should be held as either an integer or a real. If the calculation will never result in it being extremely large or gaining decimal places then an integer should be used.…
- 4285 Words
- 18 Pages
Powerful Essays -
For example, the capital letter A is ASCII character 65, the numeral 2 is ASCII 50, the character} is ASCII 125 and the metacharacter carriage return is ASCII 13. Systems based on ASCII use seven bits to represent these values digitally. In contrast, most computers store data in memory organized in eight-bit bytes. Files that contain machine-executable code and non-textual data typically contain all 256 possible eight-bit byte values. Many computer programs came to rely on this distinction between seven-bit text and eight-bit binary data, and would not function properly if non-ASCII characters appeared in data that was expected to include only ASCII text. For example, if the value of the eighth bit is not preserved, the program might interpret a byte value above 127 as a flag telling it to perform some function. It is often desirable, however, to be able to send non-textual data through text-based systems, such as when one might attach an image file to an e-mail message. To accomplish this, the data is encoded in some way, such that eight-bit data is encoded into seven-bit ASCII characters (generally using only alphanumeric and punctuation characters—the ASCII printable characters). Upon safe arrival at its destination, it is then…
- 1994 Words
- 8 Pages
Better Essays -
TRUE/FALSE. Write 'T' if the statement is true and 'F' if the statement is false.…
- 334 Words
- 5 Pages
Good Essays -
The output of an AND gate should be 1 when ALL inputs are 1. The output of an AND gate should be 0 when ANY input is 0. For more information, refer to the Week 1 Lecture or page 59 of the textbook.…
- 1751 Words
- 26 Pages
Satisfactory Essays -
In the poem “On the Subway” written by Sharon Olds, two different are being described on how society sees them based on their race. Sharon Olds describes them with traditional stereotypes with the use of tone and imagery, to differentiate yet connect the two of them.…
- 266 Words
- 2 Pages
Satisfactory Essays -
TRUE/FALSE. Write 'T' if the statement is true and 'F' if the statement is false.…
- 636 Words
- 3 Pages
Satisfactory Essays -
| What percentage of people do you think work deliberately slow to reduce the amount of work that is expected of them?…
- 3187 Words
- 13 Pages
Powerful Essays -
First of all, in the cultural area, there are some museums like the Prado, the Reina Sofia and the Thyssen where you can find several of the best works of art of the history that totally deserve a visit. The Retiro Park or the palace of Orient are others important places you can not miss.…
- 390 Words
- 2 Pages
Satisfactory Essays -
The Centre for Budget and Policy Studies has been examining government accounts to unravel the real priorities behind government policies. All governments make all kinds of promises. To keep a promise they must raise and spend money. This information is contained in budgets. What a government does, as opposed to what it says it is doing, can be gleaned from this source. But while a budget tells how money is raised [by taxes] and where it is spent, it does not tell us if the money is spent well or wisely. That requires detailed information on how projects and programmes are decided upon and audit to see how they are spent. These are complex issues of political economy. The cost of the Bangalore Metro has been stated to be 6000 crores of rupees, but it is likely to be much more. This money has come from taxes we pay to the union, the state and the city corporation. It also has a loan component from a foreign agency. And it addresses a major issue in the city. There is little doubt that Bangalore needs a mass transit system; it should have got one twenty years ago. Chinmay has documented the protest against the alignment of the Bangalore Metro in the Jayanagar area of Bangalore in mid 2009. He has attended many meetings, met many persons involved, and read the documents available. He has done a remarkable job of presenting an impartial record of what happened. This record merits careful study by all who wish to make an impact on the way large projects are designed and implemented in the country. The report, while detailed and informative, leaves many questions open. How was the Namma Metro designed? Why was the original plan, approved in 2004, confined to the boundaries of the Bangalore of 1981? The north point was Yeshwantpur, near the Indian Institute of Science, and the south point was near Rajalakshmi Nursing Home in Jayanager. By 2004, the city had…
- 6212 Words
- 25 Pages
Good Essays