(2-3) Flashcards
What will be displayed after the following statements have been executed?
Final double x;
x = 54.3;
System.out.println(“x = “ + x);
Nothing, it’s an error
What is a result of the following expression?
10 + 5 * 3 - 20
5
public class test {
public static void main(String args[] )
{
int value1 = 9;
System.out.println(value1);
int value2 = 45;
System.out.println(value2);
System.out.println(value3);
value = 16;
}
}
Nothing, it’s an error
To compile a program named First, use the following command
javac First.java
Which of the following is a valid Java statement?
String str = “John Doe.”;
Which of the following is NOT a primitive data type?
String
Given the declaration double r; which of the following statements is invalid?
r = 2.9X106;
Which of the following is NOT a rule that must be followed when naming identifiers?
Identifiers can contain spaces
To display the output on the next line, you can use the println method or use this escape sequence in the print method:
\n
When saving a Java source file save it with an extension of:
.java
This is a variable whose content is read only and cannot be changed during the program’s execution:
Named constant
A variable’s scope is the part of the program that has access to the variable
True
The boolean data type may contain values in the following range of values:
True or false
A java program must have at least one of these:
Class definition
In the following Java statement what value is stored in the variable name?
Str = “John Doe”;
The memory address where “John Doe” is located
When the + operator is used with strings, it is known as:
String concatenation operator
If x has been declared as an int, which of the following statements is invalid?
x = 1,000;
In Java, it’s standard practice to capitalize the first letter of:
Class names
Which Scanner class reads an int?
nextInt()
If the compiler encounters a statement that uses a variable before the variable is declared an error will occur.
True
Variables are classified according to their:
Data type
This is a value that is written into the code of the program
Literal
What is the result of the following code?
25 - 7 * 3 + 12 / 3
8
In Java, the beginning of a comment is marked with:
//
Which of the following is valid?
float w;
w = 1.0f;
Which of the following statements correctly creates a Scanner object for keyboard input?
Scanner keyboard = new Scanner(System.in);
What will be displayed as a result of the following code:
int x = 5, y = 20;
x += 32;
y /= 4;
System.out.println(“x = “ + x + “, y = “ + y);
x = 37, y = 5
What will be the value of z as a result of executing the following code?
int x = 5, y = 28;
float z;
z = (float) (y/x);
z = 5.0
What would be printed out as a result of the following code?
System.out.println(“The quick brown fox” + “jumped over the \n” “slow moving hen.”);
Nothing, it’s an error
What will be displayed as a result of the following code?
int x = 6
string msg = “I am enjoying this class”;
I am enjoying this class.
I AM ENJOYING THIS CLASS
i am enjoying this class
Character at index x = n msg has 25 characters
Character literals are enclosed in __________; string literals are enclosed in ____________
Single quotes, double quotes
The primitive data types only allow a(n) ___________ to hold a single value
Variable
What is the result of the following expression?
25 / 4 + 4 * 10 % 3
7
In Java ______ must be declared before it can be used
Variables
This is a named storage location in the computer’s memory
Variable
What will be displayed as a result of the following code?
int x = 578;
System.out.println(“There are” + x + 5 + “\n” + “hens in the house.”);
There are 5785 hens in the house
What will be the value of z after the following statement executes?
int x = 4, y = 33;
double z;
z = (double) (y/x);
8.0
Assuming that pay has been declared a double, the following statement is valid:
Pay = 2,888;
False
What will be the value of ans after the following code has been executed?
int x = 90, y = 55, ans = 10;
if (x == y);
ans *= 2;
20
A block of code is enclosed in a set of:
braces { }
Enclosing a group of statements in a set of braces creates a:
block of statements
When two Strings are compared using the compareTo method, the cases of the two strings are not considered
False
This type of operator determines whether a specific relationship exists between two values:
Relational
A local variable’s scope always ends at the closing brace of the block of code in which it is declared
True
What will 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 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
If chr is a character variable, which of the following if statements are written correctly?
if (chr == ‘a’)
The if/else statement will execute one group of statements if its boolean expression is true or another group if its boolean expression is false
True
_______ works like this: If the expression on the left side of the && operator is false, the expression on the right side will not be checked
Short-circuit evaluation
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
This is a boolean variable that signals when some condition exists in the program
Flag
If str1 and str2 are both strings, which of the following expressions will determine whether they are equal?
2 and 3
The statements that uses equals to and compareTo
Which of the following is the not equal operator?
!=
In most editors, you are indenting by one level each time you press this key:
tab
A Boolean expression is one that is either:
true or false
A flag may have the values
true or false
What will the values of ans, x, and y after the following statements are executed?
int ans = 0, x = 15, y = 25;
if ( x >= y)
{
ans = x + 10;
x -= y;
}
else
{
ans = y + 10;
y += x;
}
Ans = 35, x = 15, y = 40
In an if/else statement, if the boolean expression is false:
The statement or block following the else is executed
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 is the value of x after the following code has been executed?
int x = 75;
int y = 90;
if (x != y)
x += y;
165
To do a case insensitive compare which of the following could be used to test the equality of two strings str1 and str2?
(str1.equalsIgnoreCase(str2)) and (str1.compareToIgnoreCase(str2) == 0)
What would be the value of bonus after the following statements are executed?
int bonus, sales = 85000;
char dept = ‘S’;
if (sales > 100000)
if (dept == ‘R’)
bonus 2000;
else
bonus = 1500;
if (sales > 75000)
if (dept == ‘R’)
bonus 1250;
else
bonus = 1000;
else
bonus = 0;
1000
What will 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
Which of the following is the correct boolean expression to test for: int x, being a value between, but not including, 500 and 650, or int y not equal to 1000?
((x > 500 && x < 650)) || (y != 1000))
Which of the following tests the char variable chr to determine whether it is NOT equal to the character B?
if (chr != ‘B’)
Programs never need more than one path of execution
False
If you prematurely terminate an if statement with a semicolon, the compiler will
Not display and error message
Assume you are placing a null statement
Answer - All of these
What would be the value of discountRate after the following statements are executed?
double discountRate = 0.0;
int purchase = 1250;
if (purchase > 1000)
discountRate = 0.05;
if (purchase > 750)
discountRate = 0.03;
if (purchase > 500)
discountRate = 0.01;
else
bonus = 0;
0.01 (check this answer again)
The expression tested by an if statement must evaluate to:
true or false
The ________ statement is used to make simple decisions in Java
if
This operator uses two operands:
binary
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 side of the operand
True
What would be the value of bonus after the following statements are executed?
int bonus, sales = 10000;
if (sales < 5000)
bonus = 200;
else if (sales < 7500)
bonus 500;
else if (sales < 10000)
bonus = 750;
else if (sales < 20000)
bonus = 1000;
else
bonus = 1250;
1000
What would be the value of discountRate after the following statements are executed?
double discountRate = 0.0;
int purchase = 100;
if (purchase > 1000)
discountRate = 0.05;
else if (purchase > 750)
discountRate = 0.03;
else if (purchase > 500)
discountRate = 0.01;
0.0
What would be the value of discountRate after the following statements are executed?
double discountRate = 0.0;
int purchase = 1250;
char cust = ‘N’;
if (purchase > 1000)
if (char cust == ‘Y’)
discountRate = 0.05;
else discountRate = 0.04;
if (purchase > 750)
if (char cust == ‘Y’)
discountRate = 0.04;
else discountRate = 0.03;
else
discountRate = 0;
0.04 (check this answer again)
If str1 and str2 are both strings which of the following will correctly test to determine whether str1 is less than str2?
3 = (str1.compareTo(str2) < 0)