CNPP 220 - midterm
A Java program will not compile unless it contains the correct line numbers.
False
Assuming that pay has been declared a double, the following statement is valid. pay = 2,583.44;
False
Both character literals and string literals can be assigned to a char variable.
False
Compiled byte code is also called source code.
False
Identifiers and class names are examples of variables.
False
In a for loop, the control variable can only be incremented.
False
In a switch statement, if two different values for the CaseExpression would result in the same code being executed, you must have two copies of the code, one after each CaseExpression.
False
Java is a case-insensitive language.
False
Java source files end with the .class extension.
False
Most commercial software applications are large and complex and are usually developed by a single individual.
False
The contents of a variable cannot be changed while the program is running.
False
The do-while loop is a pre-test loop.
False
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 for the next iteration.
False
When two Strings are compared using the compareTo method, the cases of the two strings are not considered.
False
Which of the following will open a file named MyFile.txt and allow you to read data from it? a. File file = new File("MyFile.txt"); b. Scanner inputFile = new Scanner("MyFile.txt"); c. File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file); d. PrintWriter inputFile = new PrintWriter("MyFile.txt");
File file = new File("MyFile.txt"); Scanner inputFile = new Scanner(file);
Which of the following will open a file named MyFile.txt and allow you to append data to its existing contents?
FileWriter fwriter = new FileWriter("MyFile.txt", true); PrintWriter outFile = new PrintWriter(fwriter);
This is a boolean variable that signals when some condition exists in the program
Flag
____________ refers to the physical components that a computer is made of.
Hardware
When an object's internal data is hidden from outside code and access to that data is restricted to the object's methods, the data is protected from accidental corruption.
True
Which one of the following is the not equal operator?
!=
Which of the following is the correct boolean expression to test for: int x being a value less than or equal to 500 or greater than 650, and int y not equal to 1000?
((x <= 500 || x > 650) && !(y == 1000))
What would be the value of discountRate after the following statements are executed? double discountRate = 0.0; int purchase = 1250; if (purchase > 1000) discountRate = .05; if (purchase > 750) discountRate = .03; if (purchase > 500) discountRate = .01; else discountRate = 0;
.01
In Java, the beginning of a comment is marked with:
//
What would be the value of discountRate after the following statements are executed? double discountRate = 0.0; int purchase = 100; if (purchase > 1000) discountRate = .05; else if (purchase > 750) discountRate = .03; else if (purchase > 500) discountRate = .01;
0.0
What would be the value of discountRate after the following statements are executed? double discountRate; char custType = 'B'; switch (custType) { case 'A': discountRate = .08; break; case 'B': discountRate = .06; case 'C': discountRate = .04; default: discountRate = 0.0; }
0.0
Which of the following will format 12.78 to display as 012.8? - 000.0 - #0.00 - ###.# - ##0.0%
000.0
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100);
1
What is the value of ans after the following code has been executed? int x = 35; int y = 20, ans = 80; if (x < y); ans += y;
100
How many times will the following for loop be executed? for (int count = 10; count <= 21; count++) System.out.println("Java is great!!!");
11
What will be the value of ans after the following code has been executed? int ans = 10; int x = 65; int y = 55; if (x >= y) ans = x + y;
120
What will be the value of x after the following code is executed? int x = 75; int y = 60; if (x > y) x = x - y;
15
What will be the value of x after the following code is executed? int x, y = 15; x = y--;
15
If str1 and str2 are both Strings, which of the following expressions will correctly determine whether they are equal? (1) (str1 == str2) (2) str1.equals(str2) (3) (str1.compareTo(str2) == 0)
2 and 3
What would be the value of x after the following statements were executed? int x = 10; switch (x) { case 10: x += 15; case 12: x -= 5; break; default: x *= 3; }
20
What will be the value of charges after the following code is executed? double charges, rate = 7.00; int time = 180; charges = time <= 119 ? rate * 2 : time / 60.0 * rate;
21.00
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
What would be the value of bonus after the following statements are executed? int bonus, sales = 1250; if (sales > 1000) bonus = 100; if (sales > 750) bonus = 50; if (sales > 500) bonus = 25; else bonus = 0;
25
What will be the value of x after the following code is executed? int x, y = 4, z = 6; x = (y++) * (++z);
28
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
What will be printed when the following code is executed? double x = 45678.259; DecimalFormat formatter = new DecimalFormat("#,###,##0.00"); System.out.println(formatter.format(x));
45,678.26
What will be printed when the following code is executed? double x = 45678.259; DecimalFormat formatter = new DecimalFormat("#,##0.0"); System.out.println(formatter.format(x));
45,678.3
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100);
5
What is the result of the following expression? 10 + 5 * 3 - 20
5
What is the result of the following expression? 10 + 5 * 3 - 20
5
What will be printed after the following code is executed? for (int number = 5; number <= 15; number +=3) System.out.print(number + ", ");
5, 8, 11, 14,
What is the result of the following expression? 25 / 4 + 4 * 10 % 3
7
What is the result of the following statement? 17 % 3 * 2 - 12 + 15
7
What is the result of the following expression? 25 - 7 * 3 + 12 / 3
8
What will be the value of z after the following statements have been executed? int x = 4, y = 33; double z; z = (double) (y / x);
8.0
A Java program must have at least one of these:
Class definition
A runtime error is usually the result of:
A logical error
A computer program is:
A set of instructions that enable the computer to solve a problem or perform a task.
RAM is usually:
A volatile type of memory, used only for temporary storage
If you prematurely terminate an if statement with a semicolon, the compiler will: - Not display an error message - Assume you are placing a null statement there - All of the above - None of the above
All of the above
The major components of a typical computer system consist of: - The CPU - Input/output devices - Main memory - Secondary storage devices - All of the above
All of the above
Which of the following professions use the computer as a tool? - Accountants - Engineers - Mechanics - Nurses - All of the above
All of the above
Which of the following is not part of the programming process?
All these are parts of the programming process
Each repetition of a loop is known as what?
An iteration
Because Java byte code is the same on all computers, compiled Java programs
Are highly portable
The data contained in an object is known as
Attributes
In Java, it is standard practice to capitalize the first letter of:
Class names
In the programming process which of the following is not involved in defining what the program is to do:
Compile code
These operators use two operands:
Binary
An operating system can be categorized according to: - The number of users they can accommodate - The number of tasks they can perform at one time - Both of the above - Neither of the above
Both of the above
If you are using a block of statements, don't forget to enclose all of the statements in a set of:
Braces
A Java program must have at least one
Class definition
A loop that repeats a specific number of times is known as a(n) ________.
Counter-controlled loop
This is an item that separates other items.
Delimiter
A byte is a collection of
Eight bits
Which of the following is not a rule that must be followed when naming identifiers? - The first character must be one of the letters a-z, A-Z, and underscore or a dollar sign. - Identifiers can contain spaces. - Uppercase and lowercase characters are distinct. - After the first character, you may use the letters a-z, A-Z, the underscore, a dollar sign, or digits 0-9.
Identifiers can contain spaces.
The computer can do such a wide variety of tasks because:
It can be programmed
Predict the output of the following code: int i = 0, j = 5; int s; while (j-- > 0) { System.out.println ("J = " + j); j--; }
J = 4 J = 2 J = 0
Which of the following cannot be used as identifiers in Java?
Key words
This is a control structure that causes a statement or group of statements to repeat.
Loop
This variable controls the number of times that the loop iterates.
Loop control variable
Each different type of CPU has its own:
Machine language
Suppose you are at an operating system command line, and you are going to use the following command to compile a program: javac MyClass.java Before entering the command, you must
Make sure you are in the same directory or folder where the MyClass.java file is located
Which of the following does not describe a valid comment in Java?
Multi-line comments, start with */ and end with /*
The switch statement is a:
Multiple alternative decision structure
What does the following code do? Scanner keyboard = new Scanner(System.in); String filename; System.out.print("Enter the filename: "); filename = keyboard.readString(); PrintWriter outFile = new PrintWriter(filename);
Nothing, there is a syntax error
What will be the displayed when the following code is executed? final int x = 22, y = 4; y += x; System.out.println("x = " + x + ", y = " + y);
Nothing, this is an error
What will be displayed after the following statements have been executed? final double x; x = 54.3; System.out.println("x = " + x ); Question 9 options:
Nothing, this is an error.
The original name for Java was
Oak
This is a software entity that contains data and procedures.
Object
The two primary methods of programming in use today are
Procedural and object oriented
This is a set of programming language statements that, together, perform a specific task.
Procedure
This is a special language used to write computer programs.
Programming language
Software refers to:
Programs
Byte code instructions are: - Another name for source code - Syntax errors - Machine code instructions - Read and interpreted by the JVM
Read and interpreted by the JVM
This type of operator determines whether a specific relationship exists between two values:
Relational
Syntax is:
Rules that must be followed when writing a program
This is a sum of numbers that accumulates with each iteration of a loop.
Running total
Which of the following statements correctly creates a Scanner object for keyboard input?
Scanner keyboard = new Scanner(System.in);
These are used to indicate the end of a Java statement.
Semicolons
This is a value that signals when the end of a list of values has been reached.
Sentinel
Before entering a loop to compute a running total, the program should first
Set the accumulator where the total will be kept to an initial value, usually zero
Before entering a loop to compute a running total, the program should first do this.
Set the accumulator where the total will be kept to an initial value, usually zero
____________ works like this: If the expression on the left side of the && operator is false, the expression the right side will not be checked.
Short-circuit evaluation
The term ___________ typically refers to the device that displays console output.
Standard output device
Which of the following is not a primitive data type?
String
Java was developed by
Sun Microsystems
To print "Hello, world" on the monitor, use the following Java statement
System.out.println("Hello, world");
In most editors, you are indenting by one level each time that you press this key:
Tab
In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("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
Internally, the central processing unit (CPU) consists of two parts:
The control unit and the arithmetic and logic unit (ALU)
In the following Java statement what value is stored in the variable name? String name = "John Doe";
The memory address where "John Doe" is located
Application software refers to:
The programs that make the computer useful to the user
If the following Java statements are executed, what will be displayed? System.out.println("The top three winners are\n"); System.out.print("Jody, the Giant\n"); System.out.print("Buffy, the Barbarian"); System.out.println("Adelle, the Alligator");
The top three winners are Jody, the Giant Buffy, the BarbarianAdelle, the Alligator
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
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; do { x *= 20; } while (x > 5);
This is an infinite loop.
The purpose of validating the results of the program is:
To determine whether the program solves the original problem
A variable's scope is the part of the program that has access to the variable.
True
An important style rule you should adopt for writing if statements is to write the conditionally executed statement on the line after the if statement.
True
Because the && operator performs short-circuit evaluation, your boolean expression will usually execute faster if the subexpression that is most likely false is on the left of the && operator.
True
Because the || operator performs short-circuit evaluation, your boolean statement will generally run faster if the subexpression that is most likely to be true is on the left.
True
If the compiler encounters a statement that uses a variable before the variable is declared, an error will result.
True
In a switch statement, each of the case values must be unique.
True
Logical errors are mistakes that cause the program to produce erroneous results.
True
The Java Virtual Machine is a program that reads Java byte code instructions and executes them as they are read.
True
The do-while loop must be terminated with a semicolon.
True
Unicode is an international encoding system that is extensive enough to represent ALL the characters of ALL the world's alphabets.
True
When you pass the name of a file to the PrintWriter constructor, and the file already exists, it will be erased and a new empty file with the same name will be created.
True
Without programmers, the users of computers would have no software, and without software, computers would not be able to do anything.
True
You can use the PrintWriter class to open a file for writing and write data to it.
True
This is an international coding system that is extensive enough to represent all the characters of all the world's alphabets:
Unicode
In Java, when a character is stored in memory, it is actually as a(n) ________.
Unicode character code
This type of loop allows the user to decide the number of iterations.
User controlled loop
This is a named storage location in the computer's memory.
Variable
In Java, ___________ must be declared before they can be used.
Variables
To display the output on the next line, you can use the println method or use this escape sequence in the print method.
\n
Every Java application program must have
a method named main
In all but rare cases, loops must contain within themselves
a way to terminate
A for loop normally performs which of these steps? - Initializes a control variable to a starting value - Tests the control variable by comparing it to a maximum/minimum value and terminate when the variable reaches that value - Updates the control variable during each iteration - All of the above - None of the above
all of the above
What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y =50; if ( x >= y) { ans = x + 10; x -=y; } else { ans = y + 10; y += x; }
ans = 60, x =0, y =50
Enclosing a group of statements inside a set of braces creates a
block of statements
A block of code is enclosed in a set of
braces { }
A loop that executes as long as a particular condition exists is called a(n)
conditional loop
A loop that repeats a specific number of times is known as a(n)
counter-controlled loop
Given the following statement, which statement will write "Calvin" to the file DiskFile.txt? PrintWriter diskOut = new PrintWriter("DiskFile.txt");
diskOut.println("Calvin");
This type of loop is ideal in situations where you always want the loop to iterate at least once.
do-while loop
Variables of the boolean data type are useful for
evaluating true/false conditions
Which of the following is valid? - float y; y = 54.9; - float y; double z; z = 934.21; y = z; - float w; w = 1.0f; - float v; v = 1.0;
float w; w = 1.0f;
This type of loop is ideal in situations where the exact number of iterations is known.
for loop
The _________ statement is used to make simple decisions in Java.
if
Which of the following correctly tests the char variable chr to determine whether it is not equal to the character B?
if (chr != 'B')
If chr is a char variable, which of the following if statements is written correctly?
if (chr == 'a')
When working with the PrintWriter class, which of the following import statements should you have near the top of your program?
import java.io.*;
If a loop does not contain within itself a way to terminate, it is called a(n)
infinite loop
A sentinel value _________ and signals that there are no more values to be entered.
is a special value that cannot be mistaken as a member of the list
Which of the following will compile a program called ReadIt?
javac ReadIt.java and java ReadIt
Which Scanner class method reads an int?
nextInt()
Which Scanner class method reads a String?
nextLine()
This type of loop will always be executed at least once.
post-test loop
If x has been declared an int, which of the following statements is invalid?
r = 2.9X106;
Predict the output from the following code: j = 3; s = 2; if (j%2 == 1) { s = s + 2; } else { s = s + 4; }
s = 4
Character literals are enclosed in _____; string literals are enclosed in _____.
single quotes; double quotes
The expression in an if statement must evaluate to
true or false
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()) (...)
Which of the following expressions will determine whether x is less than or equal to y?
x <= y
Which of the following will correctly convert the data type, if x is a float and y is a double?
x = (float)y
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 values of x and y as a result of the following code? int x = 25, y = 8; x += y++;
x = 33, y = 9
What will be printed when the following code is executed? int y = 10; if ( y == 10) { int x = 30; x += y; } System.out.print("x = "); System.out.print(x); - x = 30 - x = 40 - x = 20 - x is not of scope when the last statement is executed
x is not of scope when the last statement is executed