(4-5) Flashcards

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
Q

When using the PrintWriter class, which of the following import statements would you write near the top of your program?

A

import java.io.*;

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

You can use this method to determine whether a file exists

A

The file class’s exist method

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

If you are using a block of statements, don’t forget to enclose all of the statements in a set of:

A

Braces

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

This type of loop allows the user to decide the number of iterations

A

user controlled loops

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

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.

A

True

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

What will be printed after the following code is executed?
for (int number = 5; number <= 15; number += 3)
System.out.println(number + “ , “);

A

5, 8, 11, 14

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

If a loop does not contain within itself a way to terminate, it is called a(n)

A

infinite loop

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

Java provides a set of simple unary operators designed just for incrementing and decrementing variables

A

True

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

A loop that executes as long as a particular condition exists is called a(n)

A

conditional loop

34
Q

This is a sum of numbers that accumulates with each iteration of a loop

A

Running total

35
Q

When you open a file with the PrintWriter class, the class can potentially throw an IOException

36
Q

In a for statement, the control variable can only be incremented

37
Q

Before entering a loop to compute a running total, the program should first do this:

A

Set the accumulator variable to an initial value, usually zero

38
Q

The increment operator is:

39
Q

A loop that repeats a specific number of times is known as a(n)

A

Count controlled

40
Q

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?

A

int x = 7;
displayValue(x);

41
Q

In a general sense, a method is:

A

A collection of statements that performs a certain task

42
Q

Any method that calls a method with a throws clause in its header must:

A

handle the potential exception
have the same throws clause

Answer - both of these

43
Q

A value-returning method must specify this as its return type in the header

A

any valid data type

44
Q

Two general categories of methods are void methods and value returning methods

45
Q

Constants, variables, and the values of expressions may be passed as arguments to a method

46
Q

Values that are sent into a method are called:

47
Q

A parameter’s variable scope is:

A

The method in which the parameter is declared (check)

48
Q

Which of the following values can be passed to a method that has an int parameter variable?

A

None of the above

49
Q

When you pass an argument to a method, be sure that the argument’s data type is compatible with:

A

The parameter variable’s data type

50
Q

A special variable that holds a value being passed into a method is called what?

51
Q

What will be the result of the following code?

int num;
string str = “555”;
num = Integer.parseInt(String str) + 5;

A

The last line of the code will cause an error

52
Q

This part of a method is a collection of statements that are performed when the method is executed

A

Method body

53
Q

What will be returned from the following method?

public static int methodA()
{
double a = 8.5 + 9.5;
return a;
}

A

This is an error

54
Q

Which of the following is NOT part of a method call?

A

Return type

55
Q

Given the following method header, which of the methods calls would be an error?

public void displayValues(int x, int y)

A

displayValue(a, b) where a is a short and b is a long

56
Q

Local variables can be initialized with:

A

constants
parameter values
the results of an arithmetic operator

Answer - any of these

57
Q

This type of method performs a task then terminates

58
Q

Methods are commonly used to:

A

Break a problem down into small manageable pieces

59
Q

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

60
Q

A value-returning method can return reference to a non-primitive type

61
Q

In the following code System.out.println(num) is an example of:

double num = 5.4;
System.out.println(num);
num = 0.0;

A

A void method

62
Q

This type of method performs a task and sends a value back to the code that called it.

A

Value-returning

63
Q

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);
}

A

showProduct(3.3, 55);

64
Q

In the method header the static method modifier means the method is available to code outside the class

65
Q

If you attempt to use a local variable before it has been given a value:

A

A compiler error will occur

66
Q

If method A calls method B, and method B calls method C, and method C calls method D, when method D finishes, what happens?

A

Control is returned to method C

67
Q

When an argument value is passed to a method, the receiving parameter variable is:

A

Declared in the method header inside the parentheses

68
Q

The header of a value-returning method must specify this:

A

The data type of the return type

69
Q

No statement outside the method in which a parameter variable is declared can access the parameter by its name

70
Q

Breaking a program down into small manageable methods:

A

Makes problems more easily solved
Allows for code reuse
Simplifies programs

Answer - all of the above

71
Q

You must have a return statement in a value-returning method

72
Q

Which of the following is NOT a benefit derived from using methods in programming?

A

Problems are more easily solved
Simplifies programs
Code reuse

Answer - all of these are benefits

73
Q

You should always document a method by writing comments that appear:

A

Just before the method’s definition

74
Q

Which of the following is included in a method call?

A

Parentheses

75
Q

Methods are commonly used to:

A

Break a problem down into small manageable pieces

76
Q

What is wrong with the following method call?

displayValue (double x);

A

Do not include the data type in the method call

77
Q

When a method tests an argument and returns a true or false value, it should return:

A

A boolean value

78
Q

Methods are commonly used to break a problem into small, manageable pieces

79
Q

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);
}

A

showProduct(10, 4.5);

80
Q

What will be returned from the following method?

public static double methodA()
{
double a = 8.5 + 9.5;
return a;
}