Introduction to Java Programming
Short Description
Introduction to Java ProgrammingIntroduction to Java ProgrammingIntroduction to Java ProgrammingIntroduction to Java Pro...
Description
Rudra Prasad Nayak
1 A. B. C. D.
1 / 95
Java Programming (MCQ)
________ is the physical aspect of the computer that can be seen.
Hardware Software Operating system Application program The correct answer is A
2 A. B. C. D.
__________ is the brain of a computer.
Hardware CPU Memory Disk The correct answer is B
3 A. B. C. D.
The speed of CPU is measured in __________.
megabytes gigabytes megahertz gigahertz
4
The correct answer is CD Explanation: 1 megahertz equals 1 million pulses per second and 1 gigahertz is 1000 megahertz. Why do computers use zeros and ones?
A. because combinations of zeros and ones can represent any numbers and characters. B. because digital devices have two stable states and it is natural to use one state for 0 and the other for 1. C. because binary numbers are simplest. D. because binary numbers are the bases upon which all other number systems are built. The correct answer is B
5
One byte has ________ bits.
A. 4 B. 8
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
2 / 95
Java Programming (MCQ)
C. 12 D. 16 The correct answer is B
6 A. B. C. D.
A 3.5-inch floppy can hold up to _________ bytes.
0.5 MB. 1.44 MB. 10 MB. 100 MB. The correct answer is B
7 A. B. C. D.
A regular CD-ROM can hold up to _________ bytes.
100 MB. 700 MB. 1 GB. 10 GB. The correct answer is B
8 A. B. C. D.
____________ is a device to connect a computer to a local area network (LAN).
regular modem DSL Cable modem NIC The correct answer is D
9 A. B. C. D.
____________ are instructions to the computer.
Hardware Software Programs Keyboards The correct answer is BC
10
Computer can execute the code in ____________.
A. machine language
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
3 / 95
Java Programming (MCQ)
B. assembly language C. high-level language D. none of the above The correct answer is A
11 A. B. C. D.
___________ translates high-level language program into machine language program.
An assembler A compiler CPU The operating system The correct answer is B
12 A. B. C. D. E.
____________ is an operating system.
Java C++ Windows XP Visual Basic Ada The correct answer is C
13 A. B. C. D. E.
_____________ is a program that runs on a computer to manage and control a computer's activities.
Operating system Java Modem Interpreter Compiler The correct answer is A
14 A. B. C. D. E.
Decimal number 10 is binary number ____________.
10 2 1000 1100 1010
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
4 / 95
Java Programming (MCQ)
The correct answer is E
15 A. B. C. D. E.
Decimal number 20 is hexadecimal number ____________.
A FF F1 14 1F The correct answer is D
16 A. B. C. D. E.
Binary number 1001 is decimal number ____________.
5 9 10 11 12 The correct answer is B
17 A. B. C. D. E.
Binary number 1001 is hexadecimal number ____________.
5 9 A B C The correct answer is B
18 A. B. C. D. E.
Hexadecimal number A1 is decimal number ____________.
100 101 161 162 163 The correct answer is C
19
Hexadecimal number A1 is binary number ____________.
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak A. B. C. D. E.
5 / 95
Java Programming (MCQ)
10010100 10100001 01100001 11100001 11001001 The correct answer is B
20 A. B. C. D. E.
Java was developed by ____________.
Sun Microsystems Microsoft Oracle IBM Cisco Systems The correct answer is A
21 A. B. C. D.
Java ___________ can run from a Web browser.
applications applets servlets Micro Edition programs The correct answer is B
22 A. B. C. D. E.
________ is an object-oriented programming language.
Java C++ C Ada Pascal The correct answer is AB
23 A. B. C. D. E.
________ is interpreted.
Java C++ C Ada Pascal
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
6 / 95
Java Programming (MCQ)
The correct answer is A
24 A. B. C. D. E.
________ is Architecture-Neutral.
Java C++ C Ada Pascal The correct answer is A
25
A. B. C. D.
________ is a technical definition of the language that includes the syntax and semantics of the Java programming language.
Java Java Java Java
language specification API JDK IDE
The correct answer is A
26 A. B. C. D.
________ contains predefined classes and interfaces for developing Java programs.
Java Java Java Java
language specification API JDK IDE
The correct answer is B
27
A. B. C. D.
________ consists of a set of separate programs for developing and testing Java programs, each of which is invoked from a command line. Java language specification Java API Java JDK Java IDE The correct answer is C
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
28
A. B. C. D.
7 / 95
Java Programming (MCQ)
________ provide an integrated development environment (IDE) for rapidly developing Java programs. Editing, compiling, building, debugging, and online help are integrated in one graphical user interface.
Java Java Java Java
language specification API JDK IDE
The correct answer is D
29 A. B. C. D. E.
The main method header is written as:
public public public public public
static void main(string[] args) static void Main(String[] args) static void main(String[] args) static main(String[] args) void main(String[] args)
The correct answer is C
30 A. B. C. D. E.
System.out.println('Welcome to Java'); System.out.println("Welcome to Java"); System.println('Welcome to Java'); System.out.print('Welcome to Java'); System.out.print("Welcome to Java");
31 A. B. C. D. E.
Which of the following statements is correct to display Welcome to Java on the console?
The correct answer is BE Explanation: System.out.print("...") prints the string and System.out.println("...") prints the string and moves the cursor to the new line. The JDK command to compile a class in the file Test.java is
java Test java Test.java javac Test.java javac Test JAVAC Test.java The correct answer is C
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
32 A. B. C. D. E.
8 / 95
Java Programming (MCQ)
Which JDK command is correct to run a Java application in ByteCode.class?
java ByteCode java ByteCode.class javac ByteCode.java javac ByteCode JAVAC ByteCode The correct answer is A
33 A. B. C. D.
Java compiler translates Java source code into _________.
Java bytecode machine code assembly code another high-level language code The correct answer is A
34 A. B. C. D.
_________ is a software that interprets Java bytecode.
Java Java Java Java
virtual machine compiler debugger API
The correct answer is A
35
Suppose you define a Java class as follows: public class Test { } In order to compile this program, the source code should be stored in a file named
A. B. C. D. E.
Test.class Test.doc Test.txt Test.java Any name with extension .java The correct answer is D
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
36 A. B. C. D.
9 / 95
Java Programming (MCQ)
The extension name of a Java bytecode file is
.java .obj .class .exe The correct answer is C
37 A. B. C. D.
The extension name of a Java source code file is
.java .obj .class .exe The correct answer is A
38 A. B. C. D. E.
Which of the following lines is not a Java comment?
/** comments */ // comments -- comments /* comments */ ** comments ** The correct answer is CE
39 A. B. C. D.
Which of the following are the reserved words?
public static void class The correct answer is ABCD
40 A. B. C. D.
Every statement in Java ends with ________.
a semicolon (;) a comma (,) a period (.) an asterisk (*)
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
10 / 95
Java Programming
(MCQ) The correct answer is A
41 A. B. C. D.
A block is enclosed inside __________.
Parentheses Braces Brackets Quotes The correct answer is B
42 The __________ method displays a message dialog box. A. JOptionPane.showMessage(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); B. JOptionPane.displayMessage(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); C. JOptionPane.displayMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); D. JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); E. JOptionPane.showMessageDialog(null, "Welcome to Java!"); The correct answer is DE
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
11 / 95
Java Programming
(MCQ)
1
Every letter in a Java keyword is in lowercase?
A. true B. false The correct answer is A
2 A. B. C. D. E.
Which of the following is a valid identifier?
$343 class 9X 8+9 radius The correct answer is AE
3 A. B. C. D. E.
Which of the following are correct names for variables according to Java naming conventions?
radius Radius RADIUS findArea FindArea The correct answer is AD
4 A. B. C. D.
Which of the following are correct ways to declare variables?
int int int int
length; length, length; length,
int width; width; width; int width;
The correct answer is AB
5 A. B. C. D.
____________ is the Java assignment operator.
== := = =:
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
12 / 95
Java Programming
(MCQ) The correct answer is C
6 A. B. C. D. E.
1 x x 1 x
To assign a value 1 to variable x, you write = x; = 1; := 1; := x; == 1; The correct answer is B
7 A. B. C. D.
i i i i
Which of the following assignment statements is incorrect. = j = k = 1; = 1; j = 1; k = 1; = 1 = j = 1 = k = 1; == j == k == 1; The correct answer is CD
8 A. B. C. D.
To declare a constant MAX_LENGTH inside a method with value 99.98, you write
final MAX_LENGTH = 99.98; final float MAX_LENGTH = 99.98; double MAX_LENGTH = 99.98; final double MAX_LENGTH = 99.98; The correct answer is D
9 A. B. C. D. E.
Which of the following is a constant, according to Java naming conventions?
MAX_VALUE Test read ReadInt COUNT The correct answer is AE
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
13 / 95
Java Programming
(MCQ)
10
A. B. C. D.
To improve readability and maintainability, you should declare _________ instead of using literal values such as 3.14159.
variables methods constants classes The correct answer is C
11 A. B. C. D.
Which of these data type requires the most amount of memory?
long int short byte The correct answer is A
12 A. B. C. D.
To declare an int variable number with initial value 2, you write
int int int int
number number number number
= = = =
2L; 2l; 2; 2.0;
The correct answer is C
13 A. B. C. D.
What is the result of 45 / 4?
10 11 11.25 12
14
The correct answer is B Explanation: 45 / 4 is an integer division, which results in 11 Which of the following expressions will yield 0.5?
A. 1 / 2 B. 1.0 / 2
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
14 / 95
Java Programming
(MCQ) C. (double) (1 / 2) D. (double) 1 / 2 E. 1 / 2.0
15
A. B. C. D.
The correct answer is BDE Explanation: 1 / 2 is an integer division, which results in 0. Suppose m and r are integers. What is the correct Java expression for m / r^2 to obtain a floating point result value? (r^2 denotes r raised to the power of 2).
m / m / 1.0 1.0
r * r (r * r) * m / r * r * m / (r * r)
The correct answer is D
16 A. B. C. D.
2 % 1 15 % 4 25 % 5 37 % 6
17 A. B. C. D. E.
Which of the following expression results in a value 1?
The correct answer is D Explanation: 2 % 1 is 0, 15 % 4 is 3, 25 % 5 is 0, and 37 % 6 is 1 25 % 1 is _____
1 2 3 4 0 The correct answer is E
18 A. B. C. D. E.
-25 % 5 is _____
1 2 3 4 0
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
15 / 95
Java Programming
(MCQ) The correct answer is E
19 A. B. C. D. E.
24 % 5 is _____
1 2 3 4 0 The correct answer is D
20 A. B. C. D. E.
-24 % 5 is _____
-1 -2 -3 -4 0 The correct answer is D
21 A. B. C. D. E.
-24 % -5 is _____
3 -3 4 -4 0 The correct answer is D
22 A. B. C. D. E.
1 x x x x
To add a value 1 to variable x, you write
+ x = x; += 1; := 1; = x + 1; = 1 + x; The correct answer is BDE
23
To add number to sum, you write (Note: Java is casesensitive)
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
16 / 95
Java Programming
(MCQ) A. B. C. D. E.
number += sum; number = sum + number; sum = Number + sum; sum += number; sum = sum + number; The correct answer is DE
24
Analyze the following code. public class Test { public static void main(String[] args) { int month = 09; System.out.println("month is " + month); } }
A. B. C. D.
The program displays month is 09 The program displays month is 9 The program displays month is 9.0 The program has a syntax error, because 09 is an incorrect literal value.
25
The correct answer is D Explanation: Any numeric literal with the prefix 0 is an octal value. But 9 is not an octal digit. An octal digit is 0, 1, 2, 3, 4, 5, 6, or 7. what is y displayed in the following code? public class Test1 { public static void main(String[] args) { int x = 1; int y = x = x + 1; System.out.println("y is " + y); } }
A. y is 0. B. y is 1 because x is assigned to y first. C. y is 2 because x + 1 is assigned to x and then x is assigned to y. D. The program has a syntax error since x is redeclared in the statement int y = x = x + 1. The correct answer is C Explanation: The = operator is right-associative.
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
17 / 95
Java Programming
(MCQ)
26
What is i printed? public class Test { public static void main(String[] args) { int j = 0; int i = ++j + j * 5; System.out.println("What is i? " + i); } }
A. B. C. D.
0 1 5 6
27
The correct answer is D Explanation: Operands are evaluated from left to right in Java. The left-hand operand of a binary operator is evaluated before any part of the right-hand operand is evaluated. This rule takes precedence over any other rules that govern expressions. Therefore, ++j is evaluated first, and returns 1. Then j*5 is evaluated, returns 5. What is i printed in the following code? public class Test { public static void main(String[] args) { int j = 0; int i = j++ + j * 5; System.out.println("What is i? " + i); } }
A. B. C. D.
0 1 5 6
28
The correct answer is C Explanation: Same as before, except that j++ evaluates to 0. what is y displayed in the following code? public class Test { public static void main(String[] args) { int x = 1; int y = x++ + x;
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
18 / 95
Java Programming
(MCQ) System.out.println("y is " + y); } } A. B. C. D.
y y y y
is is is is
29
1. 2. 3. 4.
The correct answer is C Explanation: When evaluating x++ + x, x++ is evaluated first, which does two things: 1. returns 1 since it is postincrement. x becomes 2. Therefore y is 1 + 2. What is y displayed? public class Test { public static void main(String[] args) { int x = 1; int y = x + x++; System.out.println("y is " + y); } }
A. B. C. D.
y y y y
is is is is
30 A. B. C. D.
x x x x
1. 2. 3. 4.
The correct answer is B Explanation: When evaluating x + x++, x is evaluated first, which is 1. X++ returns 1 since it is post-increment and 2. Therefore y is 1 + 1. To assign a double variable d to a float variable x, you write
= = = =
(long)d (int)d; d; (float)d;
The correct answer is D
31
What is the printout of the following code: double x = 5.5;
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
19 / 95
Java Programming
(MCQ) int y = (int)x; System.out.println("x is " + x + " and y is " + y); A. B. C. D. E.
x x x x x
is is is is is
32 A. B. C. D. E.
5 and y 6.0 and 6 and y 5.5 and 5.5 and
is 6 y is 6.0 is 6 y is 5 y is 5.0
The correct answer is D Explanation: The value is x is not changed after the casting. Which of the following assignment statements is illegal?
float int t short int t int t
f = s = =
= -34; 23; = 10; (int)false; 4.5;
The correct answer is DE
33 A. B. C. D. E.
What is the value of (double)5/2?
2; 2.5; 3; 2.0; 3.0; The correct answer is B
34 A. B. C. D. E.
What is the value of (double)(5/2)?
2; 2.5; 3; 2.0; 3.0; The correct answer is D
35
If you attempt to add an int, a byte, a long, and a double, the result will be a __________ value.
A. byte
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
20 / 95
Java Programming
(MCQ) B. int C. long D. double The correct answer is D
36 A. B. C. D.
4 "4" '\0004' '4'
37 A. B. C. D.
The correct answer is D Explanation: You have to write '4'. A Java character is stored in __________.
one byte two bytes three bytes four bytes
38 A. B. C. D.
Which of the following is the correct expression of character 4?
The correct answer is B Explanation: Java characters use Unicode encoding. Suppose x is a char variable with a value 'b'. What is the printout of the statement System.out.println(++x)?
a b c d The correct answer is C
39 A. B. C. D.
Which of the following statement prints smith\exam1\test.txt?
System.out.println("smith\exam1\test.txt"); System.out.println("smith\\exam1\\test.txt"); System.out.println("smith\"exam1\"test.txt"); System.out.println("smith"\exam1"\test.txt"); The correct answer is B
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
21 / 95
Java Programming
(MCQ)
40
A. B. C. D.
Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i?
System.out.println(i); System.out.println((char)i); System.out.println((int)i); System.out.println(i + " "); The correct answer is B
41 A. B. C. D.
The Unicode of 'a' is 97. What is the Unicode for 'c'?
96 97 98 99 The correct answer is D
42
Will System.out.println((char)4) display 4?
A. Yes B. No
43 A. B. C. D.
The correct answer is B Explanation: The Unicode \u0004 is to be displayed, not number 4. What is the printout of System.out.println('z' - 'a')?
25 26 a z The correct answer is A
44 A. B. C. D. E.
An int variable can hold __________.
'x' 120 120.0 "x" "120"
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
22 / 95
Java Programming
(MCQ)
45 A. B. C. D.
Which of the following assignment statements is correct?
char char char char
46 A. B. C. D. E.
The correct answer is AB Explanation: Choice (A) is also correct, because a character can be implicitly cast into an int variable. The Unicode value of character is assignment to the int variable. In this case, the code is 120 (see Appendix B).
c c c c
= = = =
'd'; 100; "d"; "100";
The correct answer is AB Explanation: Choice (B) is also correct, because an int value can be implicitly cast into a char variable. The Unicode of the character is the int value. In this case, the character is d (see Appendix B). The expression "Java " + 1 + 2 + 3 evaluates to ________.
Java123 Java6 Java 123 java 123 Illegal expression The correct answer is C
47 A. B. C. D.
Note that the Unicode for character A is 65. The expression "A" + 1 evaluates to ________.
66 B A1 Illegal expression The correct answer is C
48 A. B. C. D.
Note that the Unicode for character A is 65. The expression 'A' + 1 evaluates to ________.
66 B A1 Illegal expression
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
23 / 95
Java Programming
(MCQ) The correct answer is A
49
The __________ method displays an input dialog for reading a string.
A. String string = JOptionPane.showMessageDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); B. String string = JOptionPane.showInputDialog(null, "Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); C. String string = JOptionPane.showInputDialog("Enter a string", "Input Demo", JOptionPane.QUESTION_MESSAGE); D. String string = JOptionPane.showInputDialog(null, "Enter a string"); E. String string = JOptionPane.showInputDialog("Enter a string"); The correct answer is BDE
50 A. B. C. D.
The __________ method parses a string s to an int value.
integer.parseInt(s); Integer.parseInt(s); integer.parseInteger(s); Integer.parseInteger(s); The correct answer is B
51 A. B. C. D.
The __________ method parses a string s to a double value.
double.parseDouble(s); Double.parsedouble(s); double.parse(s); Double.parseDouble(s); The correct answer is D
52
Analyze the following code. import javax.swing.*; public class ShowErrors { public static void main(String[] args) { int i; int j; String s = JOptionPane.showInputDialog(null,
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
24 / 95
Java Programming
(MCQ) "Enter an integer", "Input", JOptionPane.QUESTION_MESSAGE); j = Integer.parseInt(s); i = (i + 4); } } A. The program cannot compile because j is not initialized. B. The program cannot compile because i does not have an initial value when it is used in i = i + 4; C. The program compiles but has a runtime error because i deos not have an initial value when it is used in i = i + 4; D. The program compiles and runs fine. The correct answer is B
53 A. B. C. D.
The __________ method returns a raised to the power of b.
Math.power(a, b) Math.exponent(a, b) Math.pow(a, b) Math.pow(b, a) The correct answer is C
54 A. B. C. D.
76.02 76 76.0252175 76.03
55 A. B. C. D.
The expression (int)(76.0252175 * 100) / 100 evaluates to _________.
The correct answer is B Explanation: In order to obtain 76.02, you have divide 100.0. The System.currentTimeMills() returns ________________ .
the current the current the current the current 1970. E. the current 1970 GMT
time. time in milliseconds. time in milliseconds since midnight. time in milliseconds since midnight, January 1, time in milliseconds since midnight, January 1, (the Unix time).
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
25 / 95
Java Programming
(MCQ) The correct answer is E
56 A. B. C. D.
Programming style is important, because ______________.
a program may not compile if it has a bad style good programming style can make a program run faster good programming style makes a program more readable good programming style helps reduce programming errors The correct answer is CD
57 A. B. C. D. E.
According to Java naming convention, which of the following names can be variables?
FindArea findArea totalLength TOTAL_LENGTH class The correct answer is BC
58
If a program compiles fine, but it produces incorrect result, then the program suffers __________. A. a compilation error B. a runtime error C. a logic error The correct answer is C
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
26 / 95
Java Programming
(MCQ)
1 A. B. C. D. E.
The "less than or equal to" comparison operator in Java is __________.
< 0) && (x > 0) (x > 0) || (x < 0) (x != 0) || (x = 0) (-10 < x < 0)
5
The correct answer is ADE Explanation: a: (3 => 4) should be (3 >= 4), d: (x = 0) should be (x == 0), and e: should be (-10 < x) && (x < 0) Which of the following is the correct expression that evaluates to true if the number x is between 1 and 100 or the number is negative?
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
27 / 95
Java Programming
(MCQ) A. B. C. D.
1 < x < 100 && x ((x < 100) && (x ((x < 100) && (x (1 > x > 100) ||
< 0 > 1)) || (x < 0) > 1)) && (x < 0) (x < 0)
The correct answer is B
6
Suppose x=10 and y=10 what is x after evaluating the expression (y > 10) & (x++ > 10).
A. 9 B. 10 C. 11
8
The correct answer is C Explanation: For the & operator, both operands are evaluated. Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) | (x++ > 10).
A. 9 B. 10 C. 11
9
The correct answer is C Explanation: For the | operator, both operands are evaluated. Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10).
A. 9 B. 10 C. 11
10 A. B. C. D.
The correct answer is B Explanation: For the || operator, the right operand is not evaluated, if the left operand is evaluated as true. _______ is the code with natural language mixed with Java code.
Java program A Java statement Pseudocode A flowchart diagram
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
28 / 95
Java Programming
(MCQ) The correct answer is C
11
Which of the following code displays the area of a circle if the radius is positive.
A. if (radius != 0) System.out.println(radius * radius * Math.PI); B. if (radius >= 0) System.out.println(radius * radius * Math.PI); C. if (radius > 0) System.out.println(radius * radius * Math.PI); D. if (radius 10) System.out.println("x is between 10 and 100");
A. The statement has syntax errors because (x 10) must be enclosed inside parentheses. B. The statement has syntax errors because (x 10) must be enclosed inside parentheses and the println(?) statement must be put inside a block. C. The statement compiles fine. D. The statement compiles fine, but has a runtime error. E. & should be replaced by && to avoid having a syntax error. The correct answer is A
13
Suppose x = 1, y = -1, and z = 1. What is the printout of the following statement? (Please indent the statement correctly first.) if (x > 0) if (y > 0) System.out.println("x > 0 and y > 0"); else if (z > 0) System.out.println("x < 0 and z > 0");
A. B. C. D.
x > 0 and y > 0; x < 0 and z > 0; x < 0 and z < 0; no printout.
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
29 / 95
Java Programming
(MCQ) The correct answer is B
14
Analyze the following code: boolean even = false; if (even = true) { System.out.println("It is even!"); }
A. B. C. D.
The The The The
15
program program program program
has a syntax error. has a runtime error. runs fine, but displays nothing. runs fine and displays It is even!.
The correct answer is D Explanation: It is a common mistake to use the = operator in the condition test. What happens is that true is assigned to even when you write even = true. So even is true. The program compiles and runs fine and prints "It is even!". Please see the Caution box in Section 3.2.3. Analyze the following code: Code 1: boolean even; if (number % 2 == 0) even = true; else even = false; Code 2: boolean even = (number % 2 == 0);
A. B. C. D.
Code Code Both Both
16
1 has syntax errors. 2 has syntax errors. Code 1 and Code 2 have syntax errors. Code 1 and Code 2 are correct, but Code 2 is better.
The correct answer is D Explanation: Please see the Tip box in Section 3.2.3. Suppose income is 4001, what is the output of the following code:
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
30 / 95
Java Programming
(MCQ) if (income > 3000) { System.out.println("Income is greater than 3000"); } else if (income > 4000) { System.out.println("Income is greater than 4000"); A. no output B. Income is C. Income is 4000 D. Income is E. Income is 3000
greater than 3000 greater than 3000 followed by Income is greater than greater than 4000 greater than 4000 followed by Income is greater than
The correct answer is B
17 A. B. C. D.
The __________ method immediately terminates the program.
System.terminate(0); System.halt(0); System.exit(0); System.stop(0); The correct answer is C
18
What is y after the following switch statement is executed? x = 3; switch (x + 3) { case 6: y = 0; case 7: y = 1; default: y += 1; }
A. B. C. D.
1 2 3 4 The correct answer is B
19
What is the printout of the following switch statement?
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
31 / 95
Java Programming
(MCQ) char ch = 'a'; switch (ch) { case 'a': case 'A': System.out.print(ch); break; case 'b': case 'B': System.out.print(ch); break; case 'c': case 'C': System.out.print(ch); break; case 'd': case 'D': System.out.print(ch); } A. B. C. D. E.
abcd a aa ab abc The correct answer is B
20
What is the printout of the following switch statement? char ch = 'b'; switch (ch) { case 'a': System.out.print(ch); case 'b': System.out.print(ch); case 'c': System.out.print(ch); case 'd': System.out.print(ch); }
A. B. C. D. E.
abcd bcd b bb bbb
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
32 / 95
Java Programming
(MCQ) The correct answer is E
21
Analyze the following program fragment: int x; double d = 1.5; switch (d) { case 1.0: x = 1; case 1.5: x = 2; case 2.0: x = 3; }
A. The program has a syntax error because the required break statement is missing in the switch statement. B. The program has a syntax error because the required default case is missing in the switch statement. C. The switch control variable cannot be double. D. No errors. The correct answer is C
22
What is y after the following statement is executed? x = 0; y = (x > 0) ? 10 : -10;
A. B. C. D. E.
-10 0 10 20 Illegal expression The correct answer is A
23
Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2:
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
33 / 95
Java Programming
(MCQ) even = (number % 2 == 0) ? true: false; Code 3: even = number % 2 == 0; A. Code 2 has a syntax error, because you cannot have true and false literals in the conditional expression. B. Code 3 has a syntax error, because you attempt to assign number to even. C. All three are correct, but Code 1 is preferred. D. All three are correct, but Code 2 is preferred. E. All three are correct, but Code 3 is preferred.
24 A. B. C. D. E.
The correct answer is E Explanation: Code 3 is the simplest. Code 1 and Code 2 contain redundant code. Which of the following are valid specifiers for the printf statement?
%4c %10b %6d %8.2d %10.2e The correct answer is ABCE
25 A. B. C. D. E.
The statement System.out.printf("%3.1f", 1234.56) outputs ___________.
123.4 123.5 1234.5 1234.56 1234.6 The correct answer is E
26 A. B. C. D. E.
The statement System.out.printf("%3.1e", 1234.56) outputs ___________.
0.1e+04 0.123456e+04 0.123e+04 1.2e+03 1.23+03
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
34 / 95
Java Programming
(MCQ) The correct answer is D
27 A. B. C. D.
The statement System.out.printf("%5d", 123456) outputs ___________.
12345 23456 123456 12345.6 The correct answer is C
28 A. B. C. D.
The statement System.out.printf("%10s", 123456) outputs ___________. (Note: * represents a space)
123456**** 23456***** 12345***** ****123456 The correct answer is D
29 A. B. C. D. E.
The order of the precedence (from high to low) of the operators +, *, &&, ||, & is:
&&, ||, &, *, + *, +, &&, ||, & *, +, &, &&, || *, +, &, ||, && &, ||, &&, *, + The correct answer is C
30 A. B. C. D. E.
Which of the following operators are right-associative.
* + % && = The correct answer is E
31
Which of the following operators are left-associative.
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
35 / 95
Java Programming
(MCQ) A. B. C. D. E.
* + % && = The correct answer is E
32
What is the value of the following expression? true | true && false
A. true B. false
34
The correct answer is B Explanation: | has higher precedence than &&, so | is evaluated first. What is the value of the following expression? true | true & false
A. true B. false The correct answer is A Explanation: & has higher precedence than |, so & is evaluated first.
35 What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0? A. true B. false C. There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true. The correct answer is C
1
How many times will the following code print "Welcome to Java"? int count = 0; while (count < 10) { System.out.println("Welcome to Java"); count++; }
A. 8
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
36 / 95
Java Programming
(MCQ) B. C. D. E.
9 10 11 0 The correct answer is C
2
How many times will the following code print "Welcome to Java"? int count = 0; while (count++ < 10) { System.out.println("Welcome to Java"); }
A. B. C. D. E.
8 9 10 11 0 The correct answer is C
3
Analyze the following code. int count = 0; while (count < 100) { // Point A System.out.println("Welcome to Java!"); count++; // Point B } // Point C
A. B. C. D. E.
count count count count count
< < < < <
100 100 100 100 100
is is is is is
always always always always always
true at Point A true at Point B false at Point B true at Point C false at Point C
The correct answer is AE
4
How many times will the following code print "Welcome to Java"?
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
37 / 95
Java Programming
(MCQ) int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10); A. B. C. D. E.
8 9 10 11 0 The correct answer is C
5
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 10);
A. B. C. D. E.
8 9 10 11 0 The correct answer is D
6
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (++count < 10);
A. B. C. D. E.
8 9 10 11 0 The correct answer is C
Copy Protected Document. Do not try to Copy
Rudra Prasad Nayak
38 / 95
Java Programming
(MCQ)
7
What is the value in count after the following loop is executed? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 9); System.out.println(count);
A. B. C. D. E.
8 9 10 11 0 The correct answer is C
8
Analyze the following statement: double sum = 0; for (double d = 0; d
View more...
Comments