(4-5) Flashcards
In the for loop, the control variable cannot be initialized to a constant value and tested against a constant value
False
What will the value of x after the following code is executed?
int x = 10;
while (x < 100)
{
x += 10;
}
100 (check answer)
A for loop normally performs which of these steps
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
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;
40
This a control statement that causes a statement or a group of statements to repeat
Loop
In all but rare cases, loops must contain within themselves:
a way to terminate
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?
while (inputFile.hasNext())
How many times will the following for loop be executed?
for (int count = 10; count <= 21; count ++)
System.out.println(“Java is great”);
12 (check answer)
What will be the value of x after the following code is executed?
int x, y = 15, z = 3;
x = (y - -) / (++z);
3
What will be the values of x and y as a result of the following code?
int x = 12, y = 5;
x += y- -;
x = 17, y = 4
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;
}
210
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();
}
The boolean condition can never be true
A file must always be opened before using it and closed when the program is finished using it
True
This type of loop is ideal in situations where the exact number of iterations is known
for loop
What will be the values of x and y as a result of the following code?
int x = 25, y = 8;
x += y++;
x = 33, y = 9 (check answer)
Which of the following are pre-test loops?
While and for
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
False
__________ is the process of inspecting data given to the program by the user and determining if it is valid
Input validation
What will be the value of x after the following code is executed?
int x = 10;
while (x < 100);
{
x +=10;
}
This is an infinite loop
What will be the value of x after the following code is executed?
int x = 10, y = 20;
while (y < 100)
{
x+= y;
}
This is an infinite loop
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?
int number = inputFile.nextInt();
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
True
Each repetition of a loop is known as what?
Iteration
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();
}
Numbers in the range 100 - 500
When using the PrintWriter class, which of the following import statements would you write near the top of your program?
import java.io.*;
You can use this method to determine whether a file exists
The file class’s exist method
If you are using a block of statements, don’t forget to enclose all of the statements in a set of:
Braces
This type of loop allows the user to decide the number of iterations
user controlled loops
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
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
If a loop does not contain within itself a way to terminate, it is called a(n)
infinite loop
Java provides a set of simple unary operators designed just for incrementing and decrementing variables
True
A loop that executes as long as a particular condition exists is called a(n)
conditional loop
This is a sum of numbers that accumulates with each iteration of a loop
Running total
When you open a file with the PrintWriter class, the class can potentially throw an IOException
True
In a for statement, the control variable can only be incremented
False
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
The increment operator is:
++
A loop that repeats a specific number of times is known as a(n)
Count controlled
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);
In a general sense, a method is:
A collection of statements that performs a certain task
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
A value-returning method must specify this as its return type in the header
any valid data type
Two general categories of methods are void methods and value returning methods
True
Constants, variables, and the values of expressions may be passed as arguments to a method
True
Values that are sent into a method are called:
Arguments
A parameter’s variable scope is:
The method in which the parameter is declared (check)
Which of the following values can be passed to a method that has an int parameter variable?
None of the above
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
A special variable that holds a value being passed into a method is called what?
Parameter
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
This part of a method is a collection of statements that are performed when the method is executed
Method body
What will be returned from the following method?
public static int methodA()
{
double a = 8.5 + 9.5;
return a;
}
This is an error
Which of the following is NOT part of a method call?
Return type
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
Local variables can be initialized with:
constants
parameter values
the results of an arithmetic operator
Answer - any of these
This type of method performs a task then terminates
Void
Methods are commonly used to:
Break a problem down into small manageable pieces
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
A value-returning method can return reference to a non-primitive type
True
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
This type of method performs a task and sends a value back to the code that called it.
Value-returning
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);
In the method header the static method modifier means the method is available to code outside the class
False
If you attempt to use a local variable before it has been given a value:
A compiler error will occur
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
When an argument value is passed to a method, the receiving parameter variable is:
Declared in the method header inside the parentheses
The header of a value-returning method must specify this:
The data type of the return type
No statement outside the method in which a parameter variable is declared can access the parameter by its name
True
Breaking a program down into small manageable methods:
Makes problems more easily solved
Allows for code reuse
Simplifies programs
Answer - all of the above
You must have a return statement in a value-returning method
True
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
You should always document a method by writing comments that appear:
Just before the method’s definition
Which of the following is included in a method call?
Parentheses
Methods are commonly used to:
Break a problem down into small manageable pieces
What is wrong with the following method call?
displayValue (double x);
Do not include the data type in the method call
When a method tests an argument and returns a true or false value, it should return:
A boolean value
Methods are commonly used to break a problem into small, manageable pieces
True
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);
What will be returned from the following method?
public static double methodA()
{
double a = 8.5 + 9.5;
return a;
}
18.0