Chapter 2 Computer Programming
T/F 'A' < 'z'
True
T/F 'P' < 'b'
True
TRUE/FALSE assignments should not repeat primitive types (int, boolean, double) double total = 0; Assignment: total = bottles * BOTTLE_VOLUME;
True
The type comes before the variable name.
True
Which are escape sequences \'' // /n \\ \n
\'' \\ \n
Which means escape? \\ or //
\\
What is this? System.out.print("Please enter the number of bottles: ");
a prompt
What does + mean in this? "bob" + 5
bob5 (concatenation)
What does + mean in this? "bob" + "cat"
bobcat (concatenation)
You must use the ___________ operator (int) to convert a floating-point value to an integer.
cast
The sequence \n, ______________
denotes a newline character
What is the most commonly used type for floating-point numbers in Java?
double
Which is the most appropriate type of variable for the value described? The average of the scores of all test takers
double
int + double =
double
int x double =
double
A string of length 0 is called: contains ____ characters is written as:
empty string no characters ""
pseudocode
english like statements to express an algorithm
The sequence \" is called a(n) ____________
escape sequence
double
floating point numbers (3.2, -2.3, 400.0)
What is the modulus operator?
%
Which one of the following operators computes the remainder of an integer division? % ! / \
%
What are the arithmetic operators?
% * / + -
If the return is: x+y ---- 2 What is the java expression?
(x + y) / 2
What does this print out? System.out.print("*\n**\n***\n");
* ** ***
What is the concatenation operator?
+
What is the value inside the value variable at the end of the given code snippet? public static void main(String[] args) { int value = 3; value = value - 2 * value; value++; } 4 2 0 -2
-2
use ______ for longer comments
/* */
use ____ to explain the purpose of a program
/** */
Which means single line comment? \\ or //
//
use ____ for short comments
//
What is the output of the following code snippet? public static void main(String[] args) { int num1 = 10; int num2 = 5; int num3 = 200; num3 = num3 % (num1 * num2); System.out.println(num3); } 10 0 4 250
0
How many of the operands need to be a string in order for it to be a concatenation?
1
int x = 5; int y = 2; double a = 3.0; double b = 1; System.out.println ((y+x)/(int) (a*y));
1
2 things about ascii table
1. A = 65 2. all uppercase are less than all lowercase
What happens to the fractional part when a division is performed on two integer variables? 1. The fractional part is discarded. 2. Two integers cannot be used in division; at least one of the operands should be a floating-point number. 3. Instead of using an integer division, you should use the modulus operator to perform floating-point division. 4. The fractional part is rounded off to the nearest integer value.
1. The fractional part is discarded.
Which statement is true? 1. When declaring a variable, you also specify the type of its values. 2. Variables cannot be assigned and declared in the same statement. 3. Variable names can be no more than 8 characters long. 4. Variable names must contain at least one dollar sign.
1. When declaring a variable, you also specify the type of its values.
Which of the following options defines an integer variable? 1. int age; 2. age: int; 3. integer age; 4. char age;
1. int age;
Which one of the following is a correct method for defining and initializing an integer variable with name value? 1. int value = 30; 2. Int value = .30; 3. int value = .30; 4. Int value = 30;
1. int value = 30;
Rules for naming variable identifiers:
1. must start with a letter (must be lowercase) 2. no spaces or special characters (except $ and __ ) 3. no reserved words (int) 4. use Camel Casing 5. make it meaningful
what will double a = 1; be stored as?
1.0
Which of the following is a valid number literal in Java? 1. 1E4 2. 3 1/2 3. 1x10^4 4. 10,000
1E4
Which statement is true about variable names in Java? 1. They can contain the percent sign (%). 2. They can contain an underscore symbol ("_"). 3. They must make sense as a word. 4. They can contain spaces.
2. They can contain an underscore symbol ("_").
Which one of the following is an assignment statement? 1. a == 20; 2. a = 20; 3. int a; 4. a := 20;
2. a = 20;
how many escape sequences?
3
Which of the following statements about constants in Java are true? I. Although not required, constants are commonly named using uppercase letters II. Only integer values can appear as constants III. A variable can be defined with an initial value, but the reserved word final prevents it from being changed IV. A named constant makes computations that use it clearer 1. I, II, and IV only 2. I, II, and III only 3. I, III, and IV only 4. II, III, and IV only
3. I, III, and IV only
int x = 5; int y = 2; double a = 3.0; double b = 1; System.out.println (y + x/y)
4
Consider the following Java variable names: I. 1stInstance II. basicInt% III. empName_ IV. addressLine1 V. DISCOUNT Which of the following options represent valid Java variable names? 1. I and IV only 2. I, IV, and V only 3. IV only 4. III, IV, and V only
4. III, IV, and V only
Which of the following statements is correct about constants? 1. Constant variables can only be changed through the Math library. 2. The data stored inside a constant can be changed using an assignment statement. 3. Constants are written using capital letters because the compiler ignores constants declared in small letters. 4. You can make a variable constant by using the final reserved word when declaring it.
4. You can make a variable constant by using the final reserved word when declaring it.
What is the output of the following code snippet? public static void main(String[] args) { int value = 25; value = value * 2; value--; System.out.println(value); } 26 49 25 50
49
What does + mean in this? 3 + 5
7 (addition)
What is the assignment operator?
=
Type
A named set of values and the operations that can be carried out with them.
Floating-point number
A number that can have a fractional part.
Integer
A number that cannot have a fractional part.
string
A sequence of characters.
characters
A single letter, digit, or symbol.
Prompt
A string that tells the user to provide input.
Variable
A symbol in a program that identifies a storage location that can hold different values.
Reserved word
A word that has a special meaning in a programming language and therefore cannot be used as a name by the programmer.
Comment
An explanation to help the human reader understand a section of a program; ignored by the compiler.
How do you get input from the user?
import java.util.scanner
What is a whole number without a fractional part in Java?
int
Which is the most appropriate type of variable for the value described? The number of questions in a quiz
int
int % int =
int
Primitive Types
int, double, char, boolean
run time error = logic error compile time error syntax error
logic error
can a double be promoted to an int?
no
What are some data types that java supports?
numbers, text strings, files, dates
char
one character ('A' , '#' , 'b')
Use the import statement to use classes from ____________.
packages
Use the assignment statement to ____________________.
place a new value into a variable
To include a quotation mark in a literal string,_______________
precede it with a backslash (\) "He said \"Hello\""
Who makes a compile time error?
programmer
When a program asks for user input, it should first print a message that tells the user which input is expected. This is called a __________
prompt
logic error =
run time error
Case sensitive
Distinguishing upper- and lowercase characters
What is the result of the following code snippet? double bottles; double bottleVolume = bottles * 2; System.out.println(bottleVolume); 1 0 2 Does not compile
Does not compile
T/F 'a' < 'M'
False
If the return is: pi sign What is the java expression?
Math.PI
If the return is: Absolute value | x | What is the method?
Math.abs(x)
If the return is: The larger of x and y What is the method?
Math.max(x, y)
If the return is: The smaller of x and y What is the method?
Math.min(x, y)
What is the java expression for _____________
Math.pow(1 + r / 100, n)
If the return is: xy (x > 0, or x = 0 and y > 0, or x < 0 and y is an integer) What is the method?
Math.pow(x, y)
If the return is: Square root of x (≥ 0) What is the method?
Math.sqrt(x)
Concatenation
Placing one string after another to form a new string.
Initialization
Setting a variable to a well-defined value when it is created.
Expression
The combination of variables, literals, operators, and/or method
Which is the most appropriate type of variable for the value described? The name of a test taker
string
a string + double =
string (a string + anything is a string)
Ascii table is a ________ of the unicode table
subset
Compile time error = run time error syntax error logic error
syntax error
compile time error =
syntax error
What does the final reserved word indicate?
that this value cannot be modified
pseudoerror
that's not a thing
What does a logic error do?
the program runs but doesn't give the wanted outcome 3+3 = 7
This is a concatenation. String fName = "Harry"; String lName = "Morgan"; String name = fName + lName; "HarryMorgan"
true
boolean
true / false
int
whole number (17,32,6,7)
If the return is: xy ---- 2 What is the java expression?
x * y / 2