Final Exam (part 1) Flashcards

1
Q

The compiler generates bytecode even if the program has syntax errors. T/F

A

False

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

Java was originally developed by a team led by James Gosling at Sun Microsystems. T/F

A

True

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

One byte has ____ bits.

A

8

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

The JDK command to compile a class in the file Text.java is _____.

A

javac Test.java

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

The command to compile a class in the file Test.java is ____.

A

javac Test.java

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

______ is a technical definition of the language that includes the syntax and semantics of the java programming language.

A

Java JDK

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

The extension name of a java source code file is _____.

A

.java

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

A block is enclosed inside _____.

A

braces

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

To assign a double variable d to a float variable x, you write ______.

A

x = (float)d;

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

-25 % 5 is _____.

A

0

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

To improve readability and maintainability, you should declare _____ instead of using literal values such as 3.14159.

A

constants

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

To assign a value 1 to variable x, you write ______.

A

x = 1;

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

Which of the following assignment statements is illegal?
float f = -34;
int t = 23;
short s = 10;
float f = 34.0;

A

float f = 34.0;

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

Any assignment statement can be used as an assignment expression. T/F

A

True

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

If you attempt to add an int, a byte, a long, and a double, the result will be a _____ value.

A

double

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

System.exit(0) can be used to terminate the program. T/F

A

True

17
Q

Suppose x = 10 and y = 10. What is x after evaluating the expression (y > 10) && (x– > 10)?

A

10

18
Q

In Java, the word true is _____.

A

a Boolean literal

19
Q

Which of the following expression is equivalent
to (x > 1)?
x >= 1
!(x <= 1)
!(x = 1)
!(x < 1)

A

!(x <= 1)

20
Q

Assume x = 4, which of the following is true?
!(x == 4)
x != 4
x == 5
x != 5

A

x != 5

21
Q

_______ are short-circuit operators.
&&

||
!

A

&&
||

22
Q

Which of the following is a possible output for (int)(51 * Math.random())?
0
50
100
500

A

0
50

23
Q

“smiles”.substring(1, 5) returns “mile”. T/F

A

true

24
Q

An int variable can hold ____.
‘x’
120
120.0

A

‘x’
120

25
Q

Which of the following is the correct expression of character 4?
4
“4”
‘\0004’

A

‘4’

26
Q

To obtain the sine of 35 degrees, use ____.
Math.sin(35)
Math.sin(Math.toRadians(35))
Math.sin(Math.toDegrees(35))
Math.sin(Math.toRadian(35))
Math.sin(Math.toDegree(35))

A

Math.sin(Math.toRadians(35))

27
Q

Given two Strings s1 = “Welcome to Java” and s2 = “Programming is fun”, which of the following is true?
s1.equals(s2)
s2.equals(s1)
s1.contains(s2)
s2.contains(s1)
!s1.contains(s2)

A

!s1.contains(s2)

28
Q

Suppose i is an int type variable. Which of the following statements display the character whose Unicode is stored in variable i?
System.out.println(i);
System.out.println((char)i);
System.out.println((int)i);
System.out.println(i + “ “);

A

System.out.println((char)i);

29
Q

Write one statement that prints out “No pain, no gain.” The output need to be in two lines as:
No pain,
no gain.
(note: there is a tab space in front of no gain)

A

System.out.println(“No pain,\n\tno gain.”);

30
Q

Write an if-else statement to print out “EVEN” if x is even; print out “ODD” if x is odd. Here x is an int type variable that has been initialized

A

if(x % 2 == 0) System.out.println(“EVEN”);
else System.out.println(“ODD”);

31
Q

Write a line of code to declare a Scanner object named keyboard.

A

Scanner keyboard = new Scanner(System.in);

32
Q

Write an if-else statement to print out “Excellent” if the student’s score is 90 or above, “Good” if the student’s score is 80 to 89, “Word harder” otherwise. Assume score is an int type variable that has been initialized.

A

if(score >=90) System.out.println(“Excellent”);
else if(score >= 80) System.out.println(“Good”);
else System.out.println(“Word harder”);

33
Q

Write a switch statement to print out “Excellent” if the student’s grade is A; “Good” if the student’s grade is B; “Word harder” otherwise. Assume grade is a char type variable that has been initialized.

A

switch(grade) {
case ‘A’: System.out.println(“Excellent”);
break;
case ‘B’: System.out.println(“Good”);
break;
default: System.out.println(“Word harder”);
break;
}

34
Q

Write one expression that returns true if and only if char c is an arithmetic operator, i.e. +, -,*,/ or %.

A

c == ‘+’ || c == ‘-‘ || c == ‘’ || c == ‘/’ || c == ‘%’
or
“+-
/%”.indexOf(c) != -1

35
Q

Write one expression that returns true if and only if String s doesn’t contain lower case English letter.

A

s.equals(s.toUpperCase())

36
Q

Write one line of code to print out the length of String greeting. Assume that greeting has been declared and initialized.

A

System.out.println(greeting.length());