1.
What is the range of an int data type in Java?
Correct Answer
C. –2^31 to 2^31 – 1
Explanation
The correct answer is -2^31 to 2^31 - 1. In Java, the int data type is a 32-bit signed two's complement integer. The range of values that can be stored in an int variable is from -2^31 to 2^31 - 1. The lower limit is -2^31 because one bit is used to represent the sign of the number. The upper limit is 2^31 - 1 because the remaining 31 bits are used to represent the magnitude of the number.
2.
What is the meaning of variable++ ?
Correct Answer
B. Add one to the variable after its current value has been used.
Explanation
The expression variable++ is a post-increment operator. It adds one to the variable after its current value has been used. This means that if the variable has a value of 5, for example, the expression variable++ would use the current value of 5 and then increment the variable to 6.
3.
Consider the following line of Java code. int x = 1+7*2;
What will be the value of x at the end of execution of the line of code?
Correct Answer
B. 15
Explanation
The value of x at the end of the execution of the line of code will be 15. This is because the multiplication operation has higher precedence than addition, so 7*2 is evaluated first, resulting in 14. Then, 1 is added to 14, giving a final value of 15 for x.
4.
The Java expression: !((b != 0) || (c <= 5)) is equivalent to:
Correct Answer
B. (b==0) && (c >5)
Explanation
The given Java expression is using the logical NOT operator (!) to negate the result of the logical OR operator (||). This means that the expression will be true only if both conditions inside the parentheses are false.
In the first option, !(b = 0) is incorrect because it is using the assignment operator (=) instead of the comparison operator (==).
In the second option, (b==0) && (c >5) is correct because it checks if b is equal to 0 and c is greater than 5.
The third and fourth options are incomplete and do not provide a valid expression.
5.
What does the following program output to the monitor:
int value = 0, count = 1;
value = count++ ;
System.out.println("value: "+ value + “count: " + count );
Correct Answer
D. Value: 1 count: 2
Explanation
The program first assigns the value of count to value and then increments the value of count by 1. Therefore, the output will be "value: 1 count: 2".
6.
What is the output of the following:
int a = 0, b = 10;
a = --b ;
System.out.println("a= " + a + " b= " + b );
Correct Answer
C. A= 9 b=9
Explanation
The output of the given code is "a= 9 b=9". This is because the expression "--b" decrements the value of b by 1 before assigning it to a. So, a becomes 9 and b also becomes 9.
7.
Which of the answers does the same thing as the following: value += sum++ ;
Correct Answer
A. Value = value + sum;
sum = sum + 1;
Explanation
The given expression "value += sum++" is equivalent to "value = value + sum; sum = sum + 1;". The expression "value += sum++" adds the value of "sum" to the value of "value", and then increments the value of "sum" by 1. This is exactly what is happening in the alternative answer "value = value + sum; sum = sum + 1;".
8.
What is the name for an application that changes a human-readable programming language into a machine-readable language?
Correct Answer
A. Compiler
Explanation
A compiler is a software application that translates a human-readable programming language into a machine-readable language. It takes the source code written by a programmer and converts it into a binary code that can be executed by a computer. This process involves multiple stages, such as lexical analysis, syntax analysis, and code generation. Ultimately, a compiler enables the computer to understand and execute the instructions written by the programmer.
9.
What kind of programming language is Java?
Correct Answer
A. An object-oriented programming language
Explanation
Java is classified as an object-oriented programming language because it is designed to organize and structure code around objects, which are instances of classes. It supports the principles of encapsulation, inheritance, and polymorphism, allowing developers to create modular and reusable code. Java also provides features like class hierarchies, interfaces, and dynamic binding, which are key components of object-oriented programming.
10.
What is the result of evaluating the following expression? (1/2 + 3.5) * 2.0
Correct Answer
A. 8.0
Explanation
The expression (1/2 + 3.5) * 2.0 can be evaluated as follows: 1/2 = 0.5 0.5 + 3.5 = 4.0 4.0 * 2.0 = 8.0 Therefore, the correct answer is 8.0.
11.
The && operator works with which data types?
Correct Answer
D. Boolean
Explanation
The && operator works with boolean data types. It is a logical operator that returns true if both operands are true, and false otherwise. It cannot be used with other data types such as int, long, or double.
12.
Which one of the following is NOT a correct variable name?
Correct Answer
A. 2bad
13.
Does every Java variable use a data type?
Correct Answer
D. Yes---each variable must be declared along with its data type.
Explanation
Every Java variable must be declared along with its data type. This is because Java is a strongly typed language, which means that variables must have a specific type assigned to them before they can be used. This allows the compiler to allocate the appropriate amount of memory for the variable and perform type checking to ensure that the variable is used correctly. Without declaring the data type of a variable, the compiler would not know how to handle it, resulting in a compilation error.
14.
When you compile a Java program, what are you doing?
Correct Answer
B. Converting it into a form the computer can better understand
Explanation
When you compile a Java program, you are converting it into a form that the computer can better understand. This process involves translating the human-readable code written in Java into machine-readable code, typically in the form of bytecode. The bytecode can then be executed by the Java Virtual Machine (JVM) on any platform, making Java programs highly portable. Compiling is an essential step in the software development process as it checks for syntax errors and produces an executable file that can be run on a computer.
15.
What is a variable?
Correct Answer
C. A place to store information in a program.
Explanation
A variable is a place to store information in a program. It is used to store and manipulate data during the execution of a program. Variables can hold different types of data such as numbers, text, or boolean values. They allow programmers to dynamically change and access data throughout the program, making it a fundamental concept in programming.
16.
What is the process of fixing errors called?
Correct Answer
D. Compiling
17.
What do you call a group of statements contained with an opening bracket and a closing bracket?
Correct Answer
A. A block statement
Explanation
A block statement refers to a group of statements enclosed within an opening bracket and a closing bracket. It is used to group multiple statements together, allowing them to be treated as a single unit. This can be helpful for organizing and controlling the flow of code execution.
18.
What is the output of the following program fragment?
for ( int j = 0; j < 5; j++ ) {
System.out.print( j + " " );
}
System.out.println( );
Correct Answer
B. 0 1 2 3 4
Explanation
The program fragment uses a for loop to iterate from 0 to 4 (since j starts at 0 and the condition j < 5 is true). Inside the loop, it prints the value of j followed by a space. After the loop, it prints a new line character. Therefore, the output will be "0 1 2 3 4".
19.
What is the output of the following code fragment?
for ( int j = 10; j > 5; j-- ) {
System.out.print( j + " " );
} System.out.println( );
Correct Answer
D. 10 9 8 7 6
Explanation
The code starts the loop with j = 10 and continues as long as j is greater than 5. In each iteration, it prints the value of j followed by a space. After the loop, it prints a new line. Therefore, the output of the code is "10 9 8 7 6".
20.
What must the test be so that the following fragment prints out the integers from 5 through 15?
for ( int j = 5; ________ ; j++ ){
System.out.print( j + " " );
}System.out.println( );
Correct Answer
C. J<16
Explanation
The test must be "j <= 15" in order for the fragment to print out the integers from 5 through 15. This is because the loop will continue as long as the condition "j <= 15" is true, and it will stop when j becomes greater than 15.
21.
What must the change be so that the following fragment prints out the even integers 0 2 4 6 8 10?
for ( int j = 0; j <= 10; _______ )
System.out.print( j + " " );
System.out.println( );
Correct Answer
B. J = j+2
Explanation
The change that needs to be made is to replace the blank with "j = j+2". This will increment the value of j by 2 in each iteration of the loop, resulting in the printing of the even integers 0 2 4 6 8 10.
22.
What must the initialization be so that the following fragment prints out the integers -3 -2 -1 ?
for ( _______; j < 0; j++ )
System.out.print( j + " " );
System.out.println( );
Correct Answer
C. Int j = -3
Explanation
The correct answer is "int j = -3". This is because the for loop condition is "j < 0", which means the loop will continue as long as j is less than 0. To print out the integers -3, -2, -1, the loop needs to start with j being equal to -3.
23.
What is the output of the following code fragment?
for ( int j = 5; j > -5; j-- )
System.out.print( j + " " );
System.out.println( );
Correct Answer
D. 5 4 3 2 1 0 -1 -2 -3 -4
Explanation
The code starts with the variable j initialized to 5. It then enters a loop that continues as long as j is greater than -5. In each iteration of the loop, it prints the value of j followed by a space. After the loop, it prints a new line.
So, the output of the code will be: 5 4 3 2 1 0 -1 -2 -3 -4.
24.
What is the output of the following code fragment?
int count = 0;
for ( ; count < 9; ++count )
System.out.print( count + " " );
Correct Answer
A. 0 1 2 3 4 5 6 7 8
Explanation
The code fragment initializes a variable "count" to 0. It then enters a for loop that continues as long as "count" is less than 9. In each iteration of the loop, "count" is incremented by 1 and the value of "count" is printed. This process continues until "count" reaches 9, at which point the loop exits. Therefore, the output of the code fragment will be the numbers 0 through 8, separated by spaces.
25.
What does this code print on the monitor?
int count = 7;
while ( count >= 4 ) {
System.out.print( count + " " ); count = count - 1;
} System.out.println( );
Correct Answer
B. 7 6 5 4
Explanation
The code initializes a variable called "count" with a value of 7. Then, it enters a while loop that continues as long as the value of "count" is greater than or equal to 4. Inside the loop, it prints the value of "count" followed by a space, and then decreases the value of "count" by 1. After the loop, it prints a new line character.
Therefore, the code will print the numbers 7, 6, 5, and 4 on the monitor, separated by spaces.
26.
What does this code print on the monitor?
int count = 1;
while ( count < 5 ) {
System.out.print( count + " " );
} System.out.println( );
Correct Answer
D. 1 1 1 1 1 1 1 1 1 1 1 . . . .
Explanation
The code initializes a variable "count" with the value 1. Then, it enters a while loop that will continue as long as "count" is less than 5. Inside the loop, it prints the value of "count" followed by a space. Since the loop does not modify the value of "count", it will always be 1. Therefore, the code will keep printing "1 " indefinitely until it is terminated.
27.
What condition should be used so that the code produce this output: 1 2 3 4 5 6 7 8
int count = 1;
while ( ___________ ) {
System.out.print( count + " " );
count = count + 1;
}
Correct Answer
B. Count < 9
Explanation
The condition "count < 9" should be used so that the code produces the output "1 2 3 4 5 6 7 8". This condition ensures that the loop will continue executing as long as the value of count is less than 9. Once count reaches 9, the condition will evaluate to false and the loop will terminate.
28.
Examine the following code fragment:
int j = 1;
while ( j < 10 ) {
System.out.println( j + " " );
j = j + j%3;
} What is output?
Correct Answer
D. 1 2 4 5 7 8
Explanation
The code fragment starts with initializing the variable j to 1. Then, it enters a while loop with the condition that j is less than 10. Inside the loop, it prints the value of j followed by a space. After that, it updates the value of j by adding the remainder of j divided by 3 to itself. This process continues until j becomes greater than or equal to 10.
Based on this logic, the output will be 1 2 4 5 7 8.
29.
Fill in the blank so that the following adds up the odd numbers from 1 to 99
int sum = 0;
for ( int num = 1; num <=99; __________ )
sum += num;
System.out.println( sum );
Correct Answer
C. Num+=2
Explanation
The correct answer is "num+=2". This is because "num+=2" is equivalent to "num = num + 2", which means that the variable "num" will increase by 2 in each iteration of the loop. By starting at 1 and increasing by 2, the loop will only consider odd numbers. Therefore, the sum variable will add up all the odd numbers from 1 to 99.
30.
Another word for "looping" is:
Correct Answer
C. Iteration
Explanation
The word "looping" refers to the process of repeating a set of instructions or actions. Similarly, the word "iteration" also means the repetition of a process or set of instructions. Therefore, "iteration" is another word for "looping".
31.
For the code below: Which are printed to standard output?
int m = 0;
while( m++ < 2 ) System.out.print(m);
Correct Answer
B. 1 2
Explanation
The code initializes a variable "m" to 0 and then enters a while loop. In each iteration of the loop, the value of "m" is incremented by 1 and then printed to the standard output. The loop continues until "m" becomes 2. Therefore, the numbers 1 and 2 are printed to the standard output.
32.
What must the test be so that the following fragment prints out the integers 5 through and including 15?
for ( int j = 5; ________ ; j++ ) {
System.out.print( j + " " );
}
Correct Answer
C. J
Explanation
The test in the blank must be "j
33.
What must the change be so that the following fragment prints out the even integers 0 2 4 6 8 10?
for ( int j = 0; j <= 10; _______ )
System.out.print( j + " " );
Correct Answer
B. J = j+2
Explanation
The correct answer is "j = j+2". This change will increment the value of j by 2 in each iteration of the loop, starting from 0. As a result, the loop will print out the even integers 0, 2, 4, 6, 8, and 10.
34.
What must the initialization be so that the following fragment prints out the integers -3 -2 -1 ?
for ( _______; j < 0; j++ )
System.out.print( j + " " );
Correct Answer
C. Int j = -3
Explanation
The correct answer is "int j = -3" because the initialization of the variable "j" needs to be set to -3 in order for the condition "j < 0" to be true. This will allow the for loop to execute and print out the integers -3, -2, and -1.
35.
Pick the for loop which duplicates this while loop:
int x = 0;
while ( x < 500 ) {
System.out.println( x );
x = x + 5; }
Correct Answer
A. for ( int x = 0; x < 500; x+=5 )
System.out.println( x );
Explanation
The correct answer is "for ( int x = 0; x < 500; x+=5 )
System.out.println( x )". This for loop has the same initialization, condition, and increment as the while loop. It starts with x = 0, continues as long as x is less than 500, and increments x by 5 in each iteration. Additionally, it prints the value of x in each iteration, just like the while loop. Therefore, this for loop duplicates the behavior of the given while loop.
36.
Pick the for loop which duplicates this while loop:
double x = 0.0;
while ( x < 100.0 ) {
System.out.println( x );
x += 0.1; }
Correct Answer
C. Double x;
for ( x = 0.0; x < 100.0; x += 0.1 )
System.out.println( x );
Explanation
The correct answer is "double x;
for ( x = 0.0; x < 100.0; x += 0.1 )
System.out.println( x );"
This for loop duplicates the while loop because it initializes a variable x as a double with a value of 0.0. It then sets the condition for the loop to continue as long as x is less than 100.0. After each iteration, it increments x by 0.1. Finally, it prints the value of x. This is exactly what the while loop does.
37.
What must the test be so that the following fragment prints out the integers -5 through and including 5?
for ( int j = -5; ________ ; j++ ){
System.out.print( j + " " ); }
Correct Answer
B. J
Explanation
The test must be "j
38.
Fill the blank so that the following fragment prints out 0.2, 0.4, 0.6, 0.8, 1.0,
for ( int j = 2; j <= 10; j+=2 )
System.out.print( __________ + ", " );
Correct Answer
D. j/10.0
Explanation
The correct answer is "j/10.0" because it divides the value of j by 10.0, which is a float, resulting in a decimal value. This will print out the desired sequence of 0.2, 0.4, 0.6, 0.8, and 1.0.
39.
What does the following statement do? String[] widget;
Correct Answer
D. It declares a variable widget which may in the future hold a reference to an array of references to String objects but is initialized to null.
Explanation
The statement "String[] widget;" declares a variable named widget which is of type String array. It indicates that the variable widget can potentially hold a reference to an array of references to String objects. However, at the moment of declaration, the variable is initialized to null, meaning it does not currently refer to any object or array.
40.
What is the difference between
String rats;
and
String[] rats; ?
Correct Answer
B. The first declares rats to be a reference to a String object, the second declares rats to be a reference to an array of String references.
Explanation
The first statement "String rats;" declares "rats" to be a reference variable that can hold a reference to a single String object. On the other hand, the second statement "String[] rats;" declares "rats" to be a reference variable that can hold a reference to an array of String references. This means that the second statement allows "rats" to refer to multiple String objects, while the first statement only allows it to refer to a single String object.
41.
What does the following statement do?
int[] values = new int[10] ;
Correct Answer
A. It declares values to be a reference to an array object and constructs an array object containing 10 integers which are initialized to zero.
Explanation
The given statement declares the variable "values" to be a reference to an array object. It also constructs an array object with a size of 10, where each element is initialized to zero.
42.
What does the following statement do?
String[] names = new String[10] ;
Correct Answer
B. It declares names to be a reference to an array of String references and constructs an array object which can contain references to 10 String objects.
Explanation
The given statement declares the variable "names" to be a reference to an array of String references. It also constructs an array object that can hold references to 10 String objects. This means that the "names" array can store up to 10 String values.
43.
Given the declaration
String[] names = new String[10] ;
Which of the following statements puts a reference to the String "Hello" in the last slot of the array?
Correct Answer
C. Names[9] = "Hello" ;
Explanation
The correct answer is names[9] = "Hello"; because arrays in Java are zero-indexed, meaning that the first element is at index 0 and the last element is at index length-1. Therefore, to put a reference to "Hello" in the last slot of the array, we need to access index 9.
44.
What types of information are arrays best suited for?
Correct Answer
A. Lists
Explanation
Arrays are best suited for storing and organizing lists of related information. Arrays allow for the efficient storage and retrieval of multiple values of the same data type, making them ideal for situations where a collection of data needs to be accessed and manipulated as a whole. Arrays provide a convenient way to keep track of and work with lists of items, such as a list of numbers, names, or any other type of data that needs to be grouped together.
45.
Which of the following statements are valid array declaration?
(A) int number();
(B) float average[];
(C) double[] marks;
(D) counter int[];
Correct Answer
A. B & C
Explanation
The valid array declarations are (B) float average[] and (C) double[] marks. In (B), an array named "average" of type float is declared. In (C), an array named "marks" of type double is declared using the array syntax. (A) int number() is not a valid array declaration as it uses parentheses instead of square brackets. (D) counter int[] is also not a valid array declaration as the type "counter" should come before the variable name.
46.
46. Consider the following code
int number[] = new int[5]; After execution of this statement, which of the following are true?
(A) number[0] is undefined
(B) number[5] is undefined
(C) number[4] is null
(D) number[2] is 0
(E) number.length() is 5
Correct Answer
A. B, D & E
Explanation
After executing the statement "int number[] = new int[5];", the following statements are true:
(B) number[5] is undefined: This is true because the array "number" has a size of 5, but the indexes of an array start from 0. Therefore, the index number[5] is out of bounds and undefined.
(D) number[2] is 0: This is true because when an array of integers is created, all the elements are initialized to their default value, which is 0 for integers. Therefore, number[2] will be 0.
(E) number.length() is 5: This is true because the length property of an array returns the number of elements in the array, which in this case is 5.
47.
47. Which of the following statements does NOT define a String array called myArray that contains five elements?
Correct Answer
D. String[5] myArray = new String();
48.
48. Which of the following declares an array of int named img?
Correct Answer
B. Int[] img;
Explanation
The correct answer is "int[] img;". This declares an array of int named "img". The square brackets "[]" after the data type "int" indicate that it is an array. The variable name "img" follows the data type and is used to reference the array.
49.
49. What are the legal indexes for the array ar, given the following declaration:
int[] ar = {2, 4, 6, 8 }
Correct Answer
A. 0, 1, 2, 3
Explanation
The legal indexes for the array ar are 0, 1, 2, and 3. This is because the array has 4 elements and the indexes start from 0 and go up to 3.
50.
What is the output of the following code fragment:
int[] ar = {2, 4, 6, 8 };
System.out.println( ar[0] + " " + ar[1] );
Correct Answer
C. 2 4
Explanation
The code fragment creates an array called "ar" with four elements: 2, 4, 6, and 8. The code then prints the value of the first element (ar[0]) followed by a space and the value of the second element (ar[1]). Therefore, the output of the code is "2 4".