WorksheetsJava - Chapter 7 - 8
Total questions: 90
Worksheet time: 3hrs 0mins
Name
Class
Date
1.
The following logical expression illustrates what concept?
!( A || B) = !A && !B
!( A || B) = !A && !B
a)
DeMorgan's Theorem
b)
Short-circuiting
c)
Mathematical expression
d)
flow control
2.
What is the difference between the "==" and "=" operators?
a)
"==" tests for equality, "=" is an assignment
b)
"=" tests for equality, "==" is an assignment
c)
"==" is used on strings, while "=" is used on numbers
3.
Which of the following expressions is the most effective way to compare the contents of two strings in myString1 and myString2 variables?
a)
myString1.equals(myString2)
b)
myString1 == myString2
c)
Both of these work equally well
d)
Neither of these will work
4.
Using the rules of operator precedence, what is the result of the following equation?
int result = 2 + 4 / 1 * 3 - 1;
int result = 2 + 4 / 1 * 3 - 1;
a)
13
b)
17
c)
12
d)
Impossible to tell without parentheses
5.
If "A" is true, short-circuiting would prevent the "B" expression from being evaluated in this
overall expression: if ( A || B )
overall expression: if ( A || B )
a)
True
b)
False
6.
What is the effect of using the equals operator == to compare two reference values?
a)
You will find out if the two values refer to exactly the same object in memory
b)
You will find out if the two objects contain essentially the same data
c)
You will get a compile-time syntax error because you must always use the equals() method to compare
reference values
reference values
d)
You may get a run-time exception if the two objects are not identical 0
7.
When using an if() statement, what must go within the parentheses?
a)
A logical expression
b)
A mathematical expression
c)
A mathematical expression
A string
A string
d)
An else clause
8.
What will be printed to the screen after the following statements execute?
if (false)
System.out.print("1");
System.out.print("2");
if (false)
System.out.print("1");
System.out.print("2");
a)
2
b)
12
c)
21
d)
nothing
9.
How many "else if ()" clauses can you have after an if() expression?
a)
0, 1, or more
b)
1 only
c)
0
d)
Up to 3
10.
Which of the following expressions are valid?
a)
if (6 + 3 > 5)
b)
if (6 + 3)
c)
if (6 + 3 = 5)
d)
They are all valid if() expressions
11.
What keyword would you use to halt the execution of a function immediately and transfer control back to the calling function?
a)
return
b)
break
c)
exit
d)
done
12.
Consider the following code:
int result = 75;
switch(result)
{
case 70:
message = "C";
break;
case 80:
message = "B";
break;
case 90:
message = "A";
break;
default:
message = "U";
break;
}
What value is in the message variable at the end of this code block?
int result = 75;
switch(result)
{
case 70:
message = "C";
break;
case 80:
message = "B";
break;
case 90:
message = "A";
break;
default:
message = "U";
break;
}
What value is in the message variable at the end of this code block?
a)
U
b)
A
c)
B
d)
C
13.
What keyword can you use within a switch() statement to mark the end of a case?
a)
break
b)
exit
c)
case
d)
done
14.
In Java 7, what values are valid as a case statement selector?
a)
numbers, letters, or strings
b)
numbers only
c)
letters only
d)
strings only
15.
Why should you be cautious about using strings in a case statement selector?
a)
Not everyone is running Java 7, which is required for this feature to work
b)
Strings that are too long won't be compared correctly
c)
The comparison is done in a case-insensitive manner, which may not be specific enough for your program.
d)
All of these are true
16.
What happens if you leave out the "break" keyword within a case statement?
a)
Program flow will continue through to the next case
b)
Program flow will skip down to the default case
c)
Program flow will skip down to after the end of the switch statement
d)
An exception will be thrown
17.
Which of the following ways to declare and initialize a for() loop index is valid?
a)
Both of these are valid
b)
for (int index=0;
c)
int index;
for (index=0;
for (index=0;
d)
Neither of these are valid
18.
Which of the following for() loops will execute 10 times?
a)
All of these will execute 10 times
b)
for (int index = 0; index < 10; index++)
c)
for (int index = 1; index <= 10; index++)
d)
for (int index = 10; index < 20; index++)
19.
Which of the following update expressions is valid to replace the ??? phrase within the for() loop below?
for (int i=0; i<10; ???)
for (int i=0; i<10; ???)
a)
All of these are valid
b)
i++
c)
i = i - 5
d)
i = i * 2
20.
What keyword can you use to terminate a for() loop early and continue program flow after the loop body?
a)
break
b)
done
c)
exit
d)
return
21.
What is the output of the following code?
for (int i = 5; i <10; i = i + 2)
{
System.out.print(i);
}
for (int i = 5; i <10; i = i + 2)
{
System.out.print(i);
}
a)
579
b)
56789
c)
5678910
d)
57911
22.
Which of the following scenarios is a good fit for a while() loop?
a)
All of these are true
b)
You want to execute a loop that does not involve a numeric index
c)
You want to build a loop that may never execute
d)
You want to execute a loop so long as some condition is true
23.
How many times will the following while() loop execute?
boolean done = true;
while (!done)
{
done = false;
}
boolean done = true;
while (!done)
{
done = false;
}
a)
0 times
b)
1 times
c)
This is an infinite loop
d)
2 times
24.
What is wrong with the following while loop?
int i = 0;
while (i < 10)
{
System.out.println(i);
}
int i = 0;
while (i < 10)
{
System.out.println(i);
}
a)
Infinite loop
b)
You cannot use a integer variable as part of a while loop expression
c)
You cannot pass an integer variable into the println() function
d)
Nothing is wrong
25.
How many times will the following do-while() loop execute?
boolean done = true;
do
{
done = false;
} while (!done);
boolean done = true;
do
{
done = false;
} while (!done);
a)
Infinite number of times
b)
0
c)
1
d)
2
26.
Which loop is always the best choice: for(), while(), or do-while()?
a)
It depends on the needs of your program
b)
for()
c)
while()
d)
do-while
27.
What three words are often used interchangeably to represent a function?
a)
function, method, subroutine
b)
function, loop, flow control
c)
function, purpose, task
d)
function, main, declaration
28.
If you put the keyword "static" in front of a function definition, what does that mean about any other functions belonging to that class you want to call from that method?
a)
Those methods must also be marked as "static"
b)
Those methods must be marked as "public"
c)
Those methods must be marked as "private"
d)
There are no restrictions on other methods belonging to the same class that you can call from the static main() method.
29.
What two symbols define the beginning and ending of the function body?
a)
{ and }
b)
( and )
c)
[ and ]
d)
/* and */
30.
Which of the following function names are invalid?
a)
void find minimum()
b)
void _find_minimum()
c)
void FINDMINIMUM()
d)
They are all invalid
31.
Which of the following locations are valid places to add a function definition?
a)
Inside a class body, but outside other function bodies
b)
Inside other function bodies
c)
Outside a class body
d)
These are all valid locations
32.
What do you call the process of breaking down a problem into smaller and smaller sub-tasks?
a)
functional decomposition
b)
mental breakdown
c)
object-oriented programming
d)
composition
33.
Which of the following tasks might be suitable for moving into a separate function so you can
re-use it from multiple places in your code?
re-use it from multiple places in your code?
a)
All of these could make good functions
b)
Ask the user to confirm an input with a yes/no answer
c)
Calculate a complex mathematical expression
d)
Sort some input data into an ordered list
34.
Which of the following functions correctly declares a double and a string parameter?
a)
void myFunction(double p1, string p2)
b)
void myFunction(double, string)
c)
void myFunction(p1, p2)
d)
void myFunction(int myDouble, int myString)
35.
What can you do with function parameters within a function body?
a)
The same things you can do with any other locally declared variable
b)
You can read the parameters but not change their local value
c)
You can write the parameters but not read their value
d)
You can change their data type
36.
Which of the following expressions correctly returns a double data type from a function?
a)
return 3.0;
b)
result 3.0;
c)
break 3.0;
d)
answer 3.0;
37.
How is it possible to declare more than one function with the same name?
a)
By using different combinations of parameter data types to uniquely identify the function
b)
It is not possible to have duplicate function names
c)
By declaring one version as public and one version as private
d)
By ensuring that each version returns a different data type
38.
What is the return data type of the following function?
public static double mystery(int myInt, float myFloat, String myString)
public static double mystery(int myInt, float myFloat, String myString)
a)
double
b)
int
c)
float
d)
String
39.
Which of the following statements correctly calls the mystery() function which was declared with a void return type?
a)
mystery()
b)
int result = mystery();
c)
if (mystery() == 3)
d)
All of these are correct
40.
If a method is declared as shown below, which of the following statements correctly calls the method?
static void mystery(double p1, string p2)
static void mystery(double p1, string p2)
a)
mystery(3.0,"hello");
b)
mystery("hello",3.0);
c)
mystery("3.0","hello");
d)
mystery(3.0,hello);
41.
When passing variables into functions as parameters, which of the following statements is true?
a)
Only the order and data types of the variable parameters are important
b)
The variable names must match the parameter names
c)
The compiler will automatically assign the correct variable value to a parameter of matching data type regardless of order
d)
You will get a run-time error if your data types don't match
42.
Given the code below, what is true about the mystery() function?
static void mystery(Object myObj)
static void mystery(Object myObj)
a)
The mystery() function will receive a copy of the object reference, which it can use to access the original object
b)
The mystery() function will receive a copy of the original Object
c)
The mystery() function will be able to modify the original myObj reference to point to a different Object
d)
None of these are true
43.
Which of the following statements are valid, given the mystery function definition below?
static int mystery(String p1)
static int mystery(String p1)
a)
All of these are valid
b)
mystery("data");
c)
int result = mystery("data");
d)
if (mystery("data") == 3)
44.
What occurs when you call a method on a reference variable that contains null?
a)
An ArrayIndexOutOfBoundsException error
b)
An ArithmeticException error
c)
A NullPointerException error
d)
A ClassCastException error
45.
What error occurs when you access an array element with an invalid index?
a)
An ArrayIndexOutOfBoundsException error
b)
An ArithmeticException error
c)
An IllegalArgumentException error
d)
A ClassCastException error
46.
What error occurs when you perform an illegal math operation such as divide-by-zero?
a)
An ArrayIndexOutOfBoundsException error
b)
An ArithmeticException error
c)
An IllegalArgumentException error
d)
A ClassCastException error
47.
What error occurs when you cast one class to another unrelated class or data type?
a)
An ArrayIndexOutOfBoundsException error
b)
An ArithmeticException error
c)
An IllegalArgumentException error
d)
A ClassCastException error
48.
What error occurs when you pass invalid input data into a method?
a)
An ArrayIndexOutOfBoundsException error
b)
An ArithmeticException error
c)
An IllegalArgumentException error
d)
A NullPointerException error
49.
What error occurs when you try to mix data types without converting correctly?
a)
An ArrayIndexOutOfBoundsException error
b)
An ArithmeticException error
c)
A type-mismatch compile-time error
d)
A NullPointerException error
50.
Given the following while() loop, which statement is true assuming A,B,C,D are int variables and A > B?
while ( ( A >= B) || ( (C - D) > 10))
{
System.out.println(A + B + C + D);
}
while ( ( A >= B) || ( (C - D) > 10))
{
System.out.println(A + B + C + D);
}
a)
The program will never enter the loop body
b)
The program will never leave the loop body
c)
The System.out.println() statement cannot convert the integers to strings
d)
The logical expression is not valid
51.
If “a = 3” and “b = 5”, what is the result of the following logical expression?
(a > b) || (b == 5)
(a > b) || (b == 5)
a)
Compiler error
b)
True
c)
False
d)
An exception will be thrown
52.
What is the result of the following mathematical expression? 2 * ( (6 + 4) / 2)
a)
10
b)
8
c)
14
d)
16
53.
What is the result of the following mathematical expression? 2 * 6 + 4 / 2
a)
10
b)
8
c)
14
d)
16
54.
What is wrong with this if() statement if “a” and “b” are both ints?
if (a = b)
if (a = b)
a)
You cannot use an assignment statement as a logical expression
b)
This will result in true no matter what the values of “a” and “b” are
c)
This will result in false no matter what the values of “a” and “b” are
d)
This will throw an exception at runtime
55.
What will be the output of the following for() loop:
for (int i=0; i<10; i+=3)
{
System.out.print(i * 3 + " ");
}
for (int i=0; i<10; i+=3)
{
System.out.print(i * 3 + " ");
}
a)
1 3 6 9 12 15 18 21 24 27 30
b)
0 3 6 9 12 15 18 21 24 27
c)
0 9 18 27
d)
0 3 6 9
56.
What will be the output of the following for() loop:
String target = "millisecond";
for (int i=0; i<target.length(); i++)
{
if (target.charAt(i) == 'l')
break;
else
System.out.print(target.charAt(i));
}
String target = "millisecond";
for (int i=0; i<target.length(); i++)
{
if (target.charAt(i) == 'l')
break;
else
System.out.print(target.charAt(i));
}
a)
m
b)
mi
c)
millisecond
d)
miisecond
57.
When might you want to use a do-while() loop instead of a while() loop?
a)
If you are sure the loop body should execute at least once
b)
When you don’t know if the loop body should execute at all
c)
When you want to more accurately control the number of loop iterations
d)
When you don’t have a valid logical expression to use
58.
When will the following code print “you can drive” ?
if (age >= 16)
{
System.out.println("you need a learner's permit");
}
else if (age >= 18)
{
System.out.println("you can drive");
}
if (age >= 16)
{
System.out.println("you need a learner's permit");
}
else if (age >= 18)
{
System.out.println("you can drive");
}
a)
Always
b)
When age is greater than or equal to 16
c)
When age is greater than or equal to 18
d)
Never
59.
Which of the following is a valid logical expression (assuming “a” and “b” are ints)?
a)
(a > (1 + b))
b)
(a == 3)
c)
((a + 3) > (b + 2))
d)
All are valid logical expressions
60.
The following logical expression illustrates what concept?
!( A || B) = !A && !B
!( A || B) = !A && !B
a)
DeMorgan's Theorem
b)
Short-circuiting
c)
Mathematical expression
Mathematical expression
Mathematical expression
d)
flow control
61.
What is the difference between the "==" and "=" operators?
a)
"==" tests for equality, "=" is an assignment
b)
"=" tests for equality, "==" is an assignment
c)
"==" is used on strings, while "=" is used on numbers
0
0
62.
When using an if() statement, what must go within the parentheses?
a)
A logical expression
b)
A mathematical expression
c)
A string
d)
An else clause
63.
How many "else if ()" clauses can you have after an if() expression?
a)
0, 1, or more
b)
1 only
c)
0
d)
Up to 3
64.
In Java 7, what values are valid as a case statement selector?
a)
numbers, letters, or strings
b)
numbers only
c)
letters only
d)
strings only
65.
What happens if you leave out the "break" keyword within a case statement?
a)
Program flow will continue through to the next case
b)
Program flow will skip down to the default case
c)
Program flow will skip down to after the end of the switch statement
d)
An exception will be thrown
66.
Which of the following for() loops will execute 10 times?
a)
All of these will execute 10 times
b)
for (int index = 0; index < 10; index++)
c)
for (int index = 1; index <= 10; index++)
d)
for (int index = 10; index < 20; index++)
67.
Which of the following update expressions is valid to replace the ??? phrase within the for() loop below?
for (int i=0; i<10; ???)
for (int i=0; i<10; ???)
a)
All of these are valid
b)
i++
c)
i = i - 5
d)
i = i * 2
68.
How many times will the following do-while() loop execute?
boolean done = true;
do
{
done = false;
} while (!done);
boolean done = true;
do
{
done = false;
} while (!done);
a)
Infinite number of times
b)
0
c)
1
d)
2
69.
Which loop is always the best choice: for(), while(), or do-while()?
a)
It depends on the needs of your program
b)
for()
c)
while()
d)
do-while()
70.
Assuming myMethod( ) returns a string, which of the following statements is valid?
a)
myMethod();
b)
System.out.println(myMethod());
c)
String result = "Return value is " + myMethod();
d)
All of these are valid
71.
Given the following function declaration:
private static double mystery(double a, int b)
{
return a * b;
}
What is the result of executing the following code?
int a = 3;
double b = 2.0;
System.out.println( mystery( a, b ));
private static double mystery(double a, int b)
{
return a * b;
}
What is the result of executing the following code?
int a = 3;
double b = 2.0;
System.out.println( mystery( a, b ));
a)
"6.0" will be printed to the console
b)
"6" will be printed to the console
c)
Nothing, there is a compile-time error
d)
Nothing, a run-time exception will be thrown
72.
Given the following method definitions:
private static void mystery(double a)
{
System.out.print("double! ");
}
private static void mystery(int a)
{
System.out.print("int! ");
}
What will be the output of the following code?
mystery(1);
mystery(1.0);
private static void mystery(double a)
{
System.out.print("double! ");
}
private static void mystery(int a)
{
System.out.print("int! ");
}
What will be the output of the following code?
mystery(1);
mystery(1.0);
a)
int! double!
b)
double! int!
c)
Duplicate function names results in a compiler error
d)
It is impossible to predict
73.
Given the following method:
private static void mystery(int a)
{
a = 3;
System.out.print(a + " ");
}
What is the result printed to the console when the following code is executed?
int a = 4;
mystery(a);
System.out.print(a + " ");
private static void mystery(int a)
{
a = 3;
System.out.print(a + " ");
}
What is the result printed to the console when the following code is executed?
int a = 4;
mystery(a);
System.out.print(a + " ");
a)
4 3
b)
3 4
c)
4 4
d)
3 3
74.
What does the following method return if a = -8.0 and b = 2.0?
private static double mystery(double a, double b)
{
if (a > 0)
return a * b;
else if (a < 0)
return a / b;
return 0;
}
private static double mystery(double a, double b)
{
if (a > 0)
return a * b;
else if (a < 0)
return a / b;
return 0;
}
a)
a * b
b)
a / b
c)
0
d)
An exception will be thrown
0
0
75.
What is wrong with the following declaration for the empty() method?
public class Tester
{
}
public static void empty()
{
}
public class Tester
{
}
public static void empty()
{
}
a)
It is not enclosed within the curly braces of the Tester class
b)
All methods other than main() must be declared as private
c)
All methods must return some value
d)
The method does not receive any input parameters
76.
What is wrong with the following function declaration?
static void errorProne(int a, double a)
return a + a;
static void errorProne(int a, double a)
return a + a;
a)
It is declared as a void but returns some data
b)
It has duplicate parameter names
c)
It is missing curly braces
d)
All of these things are wrong
77.
What keyword must be used on any method (function) on your class that is called from your main() method?
a)
static
b)
public
c)
private
d)
void
78.
Which of the following method names are invalid?
a)
_a1()
b)
_1a()
c)
1_a()
d)
a_1()
79.
Which of the following statements best reflects “functional decomposition”?
a)
Get rid of old methods you haven’t used in a while
b)
Keep your method bodies as long as possible
c)
Careful analysis of your code to ensure it is error-free
d)
Break a large task into a series of smaller, simpler methods
80.
What three words are often used interchangeably to represent a function?
a)
function, method, subroutine
b)
function, loop, flow control
c)
function, purpose, task
d)
function, main, declaration
81.
What two symbols define the beginning and ending of the function body?
a)
{ and }
b)
( and )
c)
[ and ]
d)
/* and */
82.
Which of the following locations are valid places to add a function definition?
a)
Inside a class body, but outside other function bodies
b)
Inside other function bodies
c)
Outside a class body
d)
These are all valid locations
83.
Which of the following functions correctly declares a double and a string parameter?
a)
static void myFunction(double p1, string p2)
b)
static void myFunction(double, string)
c)
static void myFunction(p1, p2)
d)
static void myFunction(int myDouble, int myString)
84.
What can you do with function parameters within a function body?
a)
The same things you can do with any other locally declared variable
b)
You can read the parameters but not change their local value
c)
You can write the parameters but not read their value
d)
You can change their data type
85.
How is it possible to declare more than one function with the same name?
a)
By using different combinations of parameter data types to uniquely identify the function
b)
It is not possible to have duplicate function names
c)
By declaring one version as public and one version as private
d)
By ensuring that each version returns a different data type
86.
What is the return data type of the following function?
public static double mystery(int myInt, float myFloat, String myString)
public static double mystery(int myInt, float myFloat, String myString)
a)
double
b)
int
c)
float
d)
String
87.
Given the code below, what is true about the mystery() function?
static void mystery(Object myObj)
static void mystery(Object myObj)
a)
The mystery() function will receive a copy of the object reference, which it can use to access the original object
b)
The mystery() function will receive a copy of the original Object
c)
The mystery() function will be able to modify the original myObj reference to point to a different Object
d)
None of these are true
88.
Which of the following statements correctly calls the mystery() function which was declared with a void return type?
a)
mystery()
b)
int result = mystery();
c)
if (mystery() == 3)
d)
All of these are correct
89.
When passing variables into functions as parameters, which of the following statements is true?
a)
Only the order and data types of the variable parameters are important
b)
The variable names must match the parameter names
c)
The compiler will automatically assign the correct variable value to a parameter of matching data type regardless of order
d)
You will get a run-time error if your data types don't match
90.
After a Java statement throws an exception, what happens to the remaining code statements after the one that threw the exception?
a)
They will execute normally, ignoring the previous exception result
b)
They will execute normally after you acknowledge the exception dialog
c)
They will execute only if the exception did not corrupt any program data
d)
Remaining statements are ignored because program flow has been transferred out of that function or your program entirely.
100 %
