Chapter 8 - Understanding Code Flashcards

1
Q
  1. What is the output of this code sequence?

double [ ] a = { 12.5, 48.3, 65.0 };
System.out.println( a[1] );

A

48.3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
  1. What is the output of this code sequence?

int [ ] a = new int [6];
System.out.println( a[4] )

A

0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What is the output of this code sequence?

double [ ] a = { 12.5, 48.3, 65.0 };
System.out.println( a.length );

A

3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. What is the output of this code sequence?

int [ ] a = { 12, 48, 65 };

for ( int i = 0; i < a.length; i++ )
System.out.println( a[i] );

A

Output of code:
12
48
65

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. What is the output of this code sequence?

int [ ] a = { 12, 48, 65 };

for ( int i = 0; i < a.length; i++ )
System.out.println( “a[” + i + “] = “ + a[i] );

A

Output of code:
a[0] = 12
a[1] = 48
a[2] = 65

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What is the output of this code sequence?

int s = 0;
int [ ] a = { 12, 48, 65 };

for ( int i = 0; i < a.length; i++ )
s += a[i];
System.out.println( “s = “ + s );

A

Output of code:

s = 125

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. What is the output of this code sequence?

int [ ] a = new int[10];

for ( int i = 0; i < a.length; i++ )
a[i] = i + 10;

System.out.println( a[4] );

A

Output of code:

14

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. What is the output of this code sequence?

double [ ] a = { 12.3, 99.6, 48.2, 65.8 };
double temp = a[0];

for ( int i = 1; i < a.length; i++ )
{
  if ( a[i] > temp )
     temp = a[i];
}
System.out.println( temp );
A

Output of code:

99.6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. What is the output of this code sequence?

int [ ] a = { 12, 48, 65, 23 };
int temp = a[1];
a[1] = a[3];
a[3] = temp;

for ( int i = 0; i < a.length; i++ )
System.out.print( a[i] + “ “ );

A
Output of code:
12
23
65
48
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. What does this method do?

public int foo( int [ ] a )
{
int temp = 0;

   for ( int i = 0; i < a.length; i++ )
   {
         if ( a[i] == 5 )
               temp++;
   }
   return temp;
}
A

It counts how many elements in the argument array have the value 5

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
  1. What does this method do?
public int foo( int [ ] a )
{
     for ( int i = 0; i < a.length; i++ )
     {
            if ( a[i] == 10 )
                 return i;
     }
     return -1;
}
A

It returns the index of the first element in the argument array with value 10. If no
element in the argument array has value 10, it returns –1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. What does this method do?
public boolean foo( int [ ] a )
{
     for ( int i = 0; i < a.length; i++ )
     {
            if ( a[i] < 0 )
                 return false;
     }
     return true;
}
A

If the argument array has at least one element that is negative, it returns false, otherwise it returns
true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
  1. What does this method do?

public String [ ] foo( String [ ] a )
{
String [ ] temp = new String[a.length];
for ( int i = 0; i < a.length; i++ )
{
temp[i] = a[i].toLowerCase( );
}
return temp;
}

A

It returns an array of Strings identical to the argument array except that the Strings
are all in lowercase.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
  1. What does this method do?

public boolean [ ] foo( String [ ] a )
{
boolean [ ] temp = new boolean[a.length];

      for ( int i = 0; i < a.length; i++ )
      {
            if (a[i].indexOf( "@" ) != -1 )
                  temp[i] = true;
            else
                  temp[i] = false;
      }
      return temp;
}
A

It returns an array of booleans as follows: if an element in the argument array
contains the character @, then the corresponding element in the returned array is
set to true, otherwise it is set to false.

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