(2-3) Flashcards

1
Q

What will be displayed after the following statements have been executed?

Final double x;
x = 54.3;
System.out.println(“x = “ + x);

A

Nothing, it’s an error

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

What is a result of the following expression?

10 + 5 * 3 - 20

A

5

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

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

A

Nothing, it’s an error

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

To compile a program named First, use the following command

A

javac First.java

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

Which of the following is a valid Java statement?

A

String str = “John Doe.”;

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

Which of the following is NOT a primitive data type?

A

String

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

Given the declaration double r; which of the following statements is invalid?

A

r = 2.9X106;

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

Which of the following is NOT a rule that must be followed when naming identifiers?

A

Identifiers can contain spaces

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

To display the output on the next line, you can use the println method or use this escape sequence in the print method:

A

\n

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

When saving a Java source file save it with an extension of:

A

.java

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

This is a variable whose content is read only and cannot be changed during the program’s execution:

A

Named constant

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

A variable’s scope is the part of the program that has access to the variable

A

True

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

The boolean data type may contain values in the following range of values:

A

True or false

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

A java program must have at least one of these:

A

Class definition

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

In the following Java statement what value is stored in the variable name?

Str = “John Doe”;

A

The memory address where “John Doe” is located

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

When the + operator is used with strings, it is known as:

A

String concatenation operator

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

If x has been declared as an int, which of the following statements is invalid?

A

x = 1,000;

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

In Java, it’s standard practice to capitalize the first letter of:

A

Class names

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

Which Scanner class reads an int?

A

nextInt()

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

If the compiler encounters a statement that uses a variable before the variable is declared an error will occur.

A

True

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

Variables are classified according to their:

A

Data type

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

This is a value that is written into the code of the program

A

Literal

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

What is the result of the following code?

25 - 7 * 3 + 12 / 3

A

8

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

In Java, the beginning of a comment is marked with:

A

//

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

Which of the following is valid?

A

float w;
w = 1.0f;

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

Which of the following statements correctly creates a Scanner object for keyboard input?

A

Scanner keyboard = new Scanner(System.in);

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

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

A

x = 37, y = 5

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

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

A

z = 5.0

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

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.”);

A

Nothing, it’s an error

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

What will be displayed as a result of the following code?

int x = 6
string msg = “I am enjoying this class”;

A

I am enjoying this class.
I AM ENJOYING THIS CLASS
i am enjoying this class
Character at index x = n msg has 25 characters

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

Character literals are enclosed in __________; string literals are enclosed in ____________

A

Single quotes, double quotes

32
Q

The primitive data types only allow a(n) ___________ to hold a single value

33
Q

What is the result of the following expression?

25 / 4 + 4 * 10 % 3

34
Q

In Java ______ must be declared before it can be used

35
Q

This is a named storage location in the computer’s memory

36
Q

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.”);

A

There are 5785 hens in the house

37
Q

What will be the value of z after the following statement executes?

int x = 4, y = 33;
double z;
z = (double) (y/x);

38
Q

Assuming that pay has been declared a double, the following statement is valid:

Pay = 2,888;

39
Q

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;

40
Q

A block of code is enclosed in a set of:

A

braces { }

41
Q

Enclosing a group of statements in a set of braces creates a:

A

block of statements

42
Q

When two Strings are compared using the compareTo method, the cases of the two strings are not considered

43
Q

This type of operator determines whether a specific relationship exists between two values:

A

Relational

44
Q

A local variable’s scope always ends at the closing brace of the block of code in which it is declared

45
Q

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;

46
Q

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;

47
Q

If chr is a character variable, which of the following if statements are written correctly?

A

if (chr == ‘a’)

48
Q

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

49
Q

_______ 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

A

Short-circuit evaluation

50
Q

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;

51
Q

This is a boolean variable that signals when some condition exists in the program

52
Q

If str1 and str2 are both strings, which of the following expressions will determine whether they are equal?

A

2 and 3

The statements that uses equals to and compareTo

53
Q

Which of the following is the not equal operator?

54
Q

In most editors, you are indenting by one level each time you press this key:

55
Q

A Boolean expression is one that is either:

A

true or false

56
Q

A flag may have the values

A

true or false

57
Q

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

A

Ans = 35, x = 15, y = 40

58
Q

In an if/else statement, if the boolean expression is false:

A

The statement or block following the else is executed

59
Q

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;

60
Q

What is the value of x after the following code has been executed?

int x = 75;
int y = 90;
if (x != y)
x += y;

61
Q

To do a case insensitive compare which of the following could be used to test the equality of two strings str1 and str2?

A

(str1.equalsIgnoreCase(str2)) and (str1.compareToIgnoreCase(str2) == 0)

62
Q

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;

63
Q

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

A

ans = 60, x = 0, y = 50

64
Q

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?

A

((x > 500 && x < 650)) || (y != 1000))

65
Q

Which of the following tests the char variable chr to determine whether it is NOT equal to the character B?

A

if (chr != ‘B’)

66
Q

Programs never need more than one path of execution

67
Q

If you prematurely terminate an if statement with a semicolon, the compiler will

A

Not display and error message
Assume you are placing a null statement

Answer - All of these

68
Q

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;

A

0.01 (check this answer again)

69
Q

The expression tested by an if statement must evaluate to:

A

true or false

70
Q

The ________ statement is used to make simple decisions in Java

71
Q

This operator uses two operands:

72
Q

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

73
Q

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;

74
Q

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;

75
Q

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;

A

0.04 (check this answer again)

76
Q

If str1 and str2 are both strings which of the following will correctly test to determine whether str1 is less than str2?

A

3 = (str1.compareTo(str2) < 0)