(4-5) Flashcards

(80 cards)

1
Q

In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What will the value of x after the following code is executed?

int x = 10;
while (x < 100)
{
x += 10;
}

A

100 (check answer)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

A for loop normally performs which of these steps

A

update the control variable during each iteration
test the control variable by comparing it to a maximum/minimum value and terminate when it reaches that value

Answer - all of the above

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What will be the value of x after the following code is executed?

int x = 10;
for (int y = 5; y < 20; y += 5)
x += y;

A

40

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

This a control statement that causes a statement or a group of statements to repeat

A

Loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

In all but rare cases, loops must contain within themselves:

A

a way to terminate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Assume that inputFile references a Scanner object that was used to open a file. Which of the following while loops shows the correct way to read data from the file until the end of the file is reached?

A

while (inputFile.hasNext())

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How many times will the following for loop be executed?

for (int count = 10; count <= 21; count ++)
System.out.println(“Java is great”);

A

12 (check answer)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What will be the value of x after the following code is executed?

int x, y = 15, z = 3;
x = (y - -) / (++z);

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What will be the values of x and y as a result of the following code?

int x = 12, y = 5;
x += y- -;

A

x = 17, y = 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What will be the value of x after the following code is executed?

int x = 10, y = 20;
while (y < 100)
{
x += y;
y += 20;
}

A

210

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In the following code, what values could be read into number to terminate the while loop?

Scanner keyboard = new Scanner(System.in);
System.out.println(“Enter a number “);
int number = keyboard.nextInt();
while (number < 100 && number > 500)
{
System.out.print(“Enter another number “);
number = keyboard.nextInt();
}

A

The boolean condition can never be true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

A file must always be opened before using it and closed when the program is finished using it

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

This type of loop is ideal in situations where the exact number of iterations is known

A

for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What will be the values of x and y as a result of the following code?

int x = 25, y = 8;
x += y++;

A

x = 33, y = 9 (check answer)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following are pre-test loops?

A

While and for

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When the break statement is encountered in a loop, all the statements in the body of the loop that appear after it are ignored, and the loop prepares of the next iteration

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

__________ is the process of inspecting data given to the program by the user and determining if it is valid

A

Input validation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What will be the value of x after the following code is executed?

int x = 10;
while (x < 100);
{
x +=10;
}

A

This is an infinite loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What will be the value of x after the following code is executed?

int x = 10, y = 20;
while (y < 100)
{
x+= y;
}

A

This is an infinite loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Assuming that inputFile references a Scanner object that was used to open a file, which of the following statements will read an int from the file?

A

int number = inputFile.nextInt();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

The while loop has two important parts: (1) the boolean expression that is tested for a true or false value, and (2) a statement or a block of statements that is repeated as long as the expression is true

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Each repetition of a loop is known as what?

A

Iteration

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

In the following code, what values could be read into number to terminate the while loop?

Scanner keyboard = new Scanner(System.in);
System.out.println(“Enter a number “);
int number = keyboard.nextInt();
while (number < 100 || number > 500)
{
System.out.println(“Enter another number: “);
keyboard.nextInt();
}

A

Numbers in the range 100 - 500

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
When using the PrintWriter class, which of the following import statements would you write near the top of your program?
import java.io.*;
26
You can use this method to determine whether a file exists
The file class's exist method
27
If you are using a block of statements, don't forget to enclose all of the statements in a set of:
Braces
28
This type of loop allows the user to decide the number of iterations
user controlled loops
29
When you pass the name of a file to the PrintWriter constructor, and the file is already exists, it will be erased and a new empty file with the same name will be created.
True
30
What will be printed after the following code is executed? for (int number = 5; number <= 15; number += 3) System.out.println(number + " , ");
5, 8, 11, 14
31
If a loop does not contain within itself a way to terminate, it is called a(n)
infinite loop
32
Java provides a set of simple unary operators designed just for incrementing and decrementing variables
True
33
A loop that executes as long as a particular condition exists is called a(n)
conditional loop
34
This is a sum of numbers that accumulates with each iteration of a loop
Running total
35
When you open a file with the PrintWriter class, the class can potentially throw an IOException
True
36
In a for statement, the control variable can only be incremented
False
37
Before entering a loop to compute a running total, the program should first do this:
Set the accumulator variable to an initial value, usually zero
38
The increment operator is:
++
39
A loop that repeats a specific number of times is known as a(n)
Count controlled
40
Assume that the following method header is for a method in class A. public void displayValue(int value) Assume that the following code segments appear in another method, also in class A. Which contains a legal call to the displayValue method?
int x = 7; displayValue(x);
41
In a general sense, a method is:
A collection of statements that performs a certain task
42
Any method that calls a method with a throws clause in its header must:
handle the potential exception have the same throws clause Answer - both of these
43
A value-returning method must specify this as its return type in the header
any valid data type
44
Two general categories of methods are void methods and value returning methods
True
45
Constants, variables, and the values of expressions may be passed as arguments to a method
True
46
Values that are sent into a method are called:
Arguments
47
A parameter's variable scope is:
The method in which the parameter is declared (check)
48
Which of the following values can be passed to a method that has an int parameter variable?
None of the above
49
When you pass an argument to a method, be sure that the argument's data type is compatible with:
The parameter variable's data type
50
A special variable that holds a value being passed into a method is called what?
Parameter
51
What will be the result of the following code? int num; string str = "555"; num = Integer.parseInt(String str) + 5;
The last line of the code will cause an error
52
This part of a method is a collection of statements that are performed when the method is executed
Method body
53
What will be returned from the following method? public static int methodA() { double a = 8.5 + 9.5; return a; }
This is an error
54
Which of the following is NOT part of a method call?
Return type
55
Given the following method header, which of the methods calls would be an error? public void displayValues(int x, int y)
displayValue(a, b) where a is a short and b is a long
56
Local variables can be initialized with:
constants parameter values the results of an arithmetic operator Answer - any of these
57
This type of method performs a task then terminates
Void
58
Methods are commonly used to:
Break a problem down into small manageable pieces
59
Any method that calls a method with a throws clause in its header must either handle the potential exception or have the same throws clause
True
60
A value-returning method can return reference to a non-primitive type
True
61
In the following code System.out.println(num) is an example of: double num = 5.4; System.out.println(num); num = 0.0;
A void method
62
This type of method performs a task and sends a value back to the code that called it.
Value-returning
63
Which of the following would be a valid method call for the following method? public static void showProduct(double num1, int num2) { double product; product = num1 * num2; System.out.println("The product is " + product); }
showProduct(3.3, 55);
64
In the method header the static method modifier means the method is available to code outside the class
False
65
If you attempt to use a local variable before it has been given a value:
A compiler error will occur
66
If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens?
Control is returned to method C
67
When an argument value is passed to a method, the receiving parameter variable is:
Declared in the method header inside the parentheses
68
The header of a value-returning method must specify this:
The data type of the return type
69
No statement outside the method in which a parameter variable is declared can access the parameter by its name
True
70
Breaking a program down into small manageable methods:
Makes problems more easily solved Allows for code reuse Simplifies programs Answer - all of the above
71
You must have a return statement in a value-returning method
True
72
Which of the following is NOT a benefit derived from using methods in programming?
Problems are more easily solved Simplifies programs Code reuse Answer - all of these are benefits
73
You should always document a method by writing comments that appear:
Just before the method's definition
74
Which of the following is included in a method call?
Parentheses
75
Methods are commonly used to:
Break a problem down into small manageable pieces
76
What is wrong with the following method call? displayValue (double x);
Do not include the data type in the method call
77
When a method tests an argument and returns a true or false value, it should return:
A boolean value
78
Methods are commonly used to break a problem into small, manageable pieces
True
79
Which of the following would be a valid method call for the following method? public static void showProduct(int num1, double num2) { int product; product = num1 * int(num2); System.out.println("The product is" + product); }
showProduct(10, 4.5);
80
What will be returned from the following method? public static double methodA() { double a = 8.5 + 9.5; return a; }
18.0