Data Types and Operators Flashcards

1
Q

Why does Java strictly specify the range and behavior of its primitive types?

A

Java strictly specifies the range and behavior of its primitive types to ensure portability across platforms.

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

What is Java’s character type, and how does it differ from the character type used by some other programming languages?

A

Java’s character type is char. Java characters are Unicode rather than ASCII, which is used by some other computer languages.

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

A boolean value can have any value you like because any non-zero value is true. True or False?

A

False. A boolean value must be either true or false.

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

Given this output,

One
Two
Three

using a single string, show the println( ) statement that produced it.

A

System.out.println(“One\nTwo\nThree”);

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

What is wrong with this fragment?

for(i = 0; i < 10; i++) {
int sum;

sum = sum + i;
}
System.out.println(“Sum is: “ + sum);

A

There are two fundamental flaws in the fragment.

First, sum is created each time the block defined by the for loop is entered and destroyed on exit. Thus, it will not hold its value between iterations. Attempting to use sum to hold a running sum of the iterations is pointless.

Second, sum will not be known outside of the block in which it is declared. Thus, the reference to it in the println( ) statement is invalid.

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

Explain the difference between the prefix and postfix forms of the increment operator.

A

When the increment operator precedes its operand, Java will perform the increment prior to obtaining the operand’s value for use by the rest of the expression. If the operator follows its operand, then Java will obtain the operand’s value before incrementing.

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

Show how a short-circuit AND can be used to prevent a divide-by-zero error.

A

if((b != 0) && (val / b)) …

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

In an expression, what type are byte and short promoted to?

A

In an expression, byte and short are promoted to int.

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

In general, when is a cast needed?

A

A cast is needed when converting between incompatible types or when a narrowing conversion is occurring.

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

Write a program that finds all of the prime numbers between 2 and 100.

A

// Find prime numbers between 2 and 100.
class Prime {
public static void main(String[] args) {
int i, j;
boolean isprime;

for(i=2; i < 100; i++) {
  isprime = true;

  // see if the number is evenly divisible
  for(j=2; j <= i/j; j++)
    // if it is, then it's not prime
    if((i%j) == 0) isprime = false;

  if(isprime)
    System.out.println(i + " is prime.");
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Does the use of redundant parentheses affect program performance?

A

No.

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

Does a block define a scope?

A

Yes.

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