More Data Types and Operators Flashcards

1
Q

Show two ways to declare a one-dimensional array of 12 doubles.

A

double x[] = new double[12];
double[] x = new double[12];

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

Show how to initialize a one-dimensional array of integers to the values 1 through 5.

A

int[] x = { 1, 2, 3, 4, 5 };

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

Write a program that uses an array to find the average of ten double values. Use any ten
values you like.

A

// Average 10 double values.
class Avg {
public static void main(String[] args) {
double[] nums = { 1.1, 2.2, 3.3, 4.4, 5.5,
6.6, 7.7, 8.8, 9.9, 10.1 };
double sum = 0;

for(int i=0; i < nums.length; i++)
  sum += nums[i];

System.out.println("Average: " + sum / nums.length);   } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Change the sort in Try This 5-1 so that it sorts an array of strings. Demonstrate that it works.

A

// Demonstrate the Bubble sort with strings.
class StrBubble {
public static void main(String[] args) {
String[] strs = {
“this”, “is”, “a”, “test”,
“of”, “a”, “string”, “sort”
};
int a, b;
String t;
int size;

size = strs.length; // number of elements to sort

// display original array
System.out.print("Original array is:");
for(int i=0; i < size; i++)
  System.out.print(" " + strs[i]);
System.out.println();

// This is the bubble sort for strings.
for(a=1; a < size; a++)
  for(b=size-1; b >= a; b--) {
    if(strs[b-1].compareTo(strs[b]) > 0) { // if out of order
      // exchange elements
      t = strs[b-1];
      strs[b-1] = strs[b]; 
      strs[b] = t; 
    }
  } // display sorted array
System.out.print("Sorted array is:");
for(int i=0; i < size; i++)
  System.out.print(" " + strs[i]);
System.out.println();   } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between the String methods indexOf( ) and lastIndexOf( )?

A

The indexOf( ) method finds the first occurrence of the specified substring. lastIndexOf( ) finds the
last occurrence.

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

Since all strings are objects of type String, show how you can call the length( ) and charAt( ) methods on this string literal: “I like Java”.

A

As strange as it may look, this is a valid call to length( ):
System.out.println(“I like Java”.length());

The output displayed is 11. charAt( ) is called in a similar fashion.

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

Expanding on the Encode cipher class, modify it so that it uses an eight-character string as
the key.

A

// An improved XOR cipher.
class Encode {
public static void main(String[] args) {
String msg = “This is a test”;
String encmsg = “”;
String decmsg = “”;
String key = “abcdefgi”;
int j;

System.out.print("Original message: ");
System.out.println(msg);

// encode the message
j = 0;
for(int i=0; i < msg.length(); i++) {
  encmsg = encmsg + (char) (msg.charAt(i) ^ key.charAt(j));
  j++;
  if(j==8) j = 0;
}

System.out.print("Encoded message: ");
System.out.println(encmsg); 
 
// decode the message
j = 0;
for(int i=0; i < msg.length(); i++) {
  decmsg = decmsg + (char) (encmsg.charAt(i) ^ key.charAt(j)); j++;
  if(j==8) j = 0;
}

System.out.print("Decoded message: ");
System.out.println(decmsg);   } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can the bitwise operators be applied to the double type?

A

No.

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

Show how this sequence can be rewritten using the ? operator.

if(x < 0) y = 10;
else y = 20;

A

Here is the answer:
y = x < 0 ? 10 : 20;

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

In the following fragment, is the & a bitwise or logical operator? Why?

boolean a, b;
// …
if(a & b) …

A

It is a logical operator because the operands are of type boolean.

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

Is it an error to overrun the end of an array?

Is it an error to index an array with a negative value?

A

Yes.

Yes. All array indexes start at zero.

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

What is the unsigned right-shift operator?

A

> > >

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

Rewrite the MinMax class shown earlier in this chapter so that it uses a for-each style for loop.

A

// Find the minimum and maximum values in an array.
class MinMax {
public static void main(String[] args) {
int[] nums = new int[10];
int min, max;

nums[0] = 99;
nums[1] = -10;
nums[2] = 100123;
nums[3] = 18;
nums[4] = -978;
nums[5] = 5623; 
nums[6] = 463;
nums[7] = -9;
nums[8] = 287;
nums[9] = 49;

min = max = nums[0];
for(int v : nums) {
  if(v < min) min = v;
  if(v > max) max = v; 
} 
System.out.println("min and max: " + min + " " + max);   } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Can the for loops that perform sorting in the Bubble class shown in Try This 5-1 be converted into for-each style loops? If not, why not?

A

No, the for loops in the Bubble class that perform the sort cannot be converted into for-each style loops. In the case of the outer loop, the current value of its loop counter is needed by the inner loop. In the case of the inner loop, out-of-order values must be exchanged, which implies assignments. Assignments to the underlying array cannot take place when using a for-each style loop.

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

Can a String control a switch statement?

A

Yes.

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

What keyword is reserved for use with local variable type inference?

A

The context-sensitive keyword var is reserved for use with local variable type inference.

17
Q

Show how to use local variable type inference to declare a boolean variable called done that has an initial value of false.

A

var done = false;

18
Q

Can var be the name of a variable? Can var be the name of a class?

A

Yes, var can be the name of a variable. No, var cannot be the name of a class.

19
Q

Is the following declaration valid? If not, why not.

var[] avgTemps = new double[7];

A

No, it is not valid because array brackets are not allowed after var. Remember, the complete type is
inferred from the initializer.

20
Q

Is the following declaration valid? If not, why not?

var alpha = 10, beta = 20;

A

No, only one variable at a time can be declared when type inference is used.

21
Q

In the show( ) method of the ShowBits class developed in Try This 5-3, the local variable
mask is declared as shown here:

long mask = 1;

Change this declaration so that it uses local variable type inference. When doing so, be sure that mask is of type long (as it is here), and not of type int.

A

var mask = 1L; // Notice that the initial value is explicitly
// specified as long so that mask will be
// inferred to be long.