Name: 
 

Java09 Review pt 2d



True/False
Indicate whether the statement is true or false.
 

 1. 

The isUpperCase( ) method returns the uppercase equivalent of an argument.
 

 2. 

The first position of a String object begins with 1.
 

 3. 

Array subscripts may have floating-point values.
 

 4. 

You can declare arrays that hold elements of any type.
 

Multiple Choice
Identify the choice that best completes the statement or answers the question.
 

 5. 

Which of the following would you use to compare the contents of two Strings?
a.
equals( )
c.
isEqual( )
b.
==
d.
=
 

 6. 

If two String objects have identical contents, equals( ) returns ____.
a.
1
c.
0
b.
false
d.
true
 

 7. 

What is the output of the following program?

public class Test {
   public static void main(String[] args) {
       String s1 = "Testing";
       String s2 = "testing";
       String s3 = s1.toLowerCase();
       if (s2.equals(s3))
         System.out.println("true");
       else
         System.out.println("false");
   }
}
a.
true
c.
Testing
b.
false
d.
testing
 

 8. 

Which of the following would you use to compare strings without considering case?
a.
==
c.
equals( )
b.
equalsIgnoreCase( )
d.
isEqual( )
 

 9. 

The output of the following program is ____.

public class Test {
  public static void main(String[] args) {
      String s1 = "Programs";
      String s2 = "programs";
      if (s1.equalsIgnoreCase(s2))
         System.out.println("True");
      else
         System.out.println("False");
  }
}
a.
programs
c.
False
b.
True
d.
Programs
 

 10. 

The value of n after the following program executes is ____.

public class Test {
  public static void main(String[] args) {
      String s1 = "Programs";
      String s2 = "Progress";
      int n = s1.compareTo(s2);
      System.out.println(n);
  }
}
a.
0
c.
positive value
b.
false
d.
negative value
 

 11. 

The output of the following program is ____.

public class Test {
  public static void main(String[] args) {
    String name = "Jack Smith";
    int i = name.indexOf(' ');
    System.out.println("i = " + i);
  }
}
a.
i = -1
c.
i = 4
b.
i = 3
d.
i = 5
 

 12. 

What is the value of c after the following code executes?

     String s = "Application";
     char c = s.charAt(5);
a.
i
c.
a
b.
l
d.
c
 

 13. 

What is the value of result?

    String s = "Programming";
    boolean result = s.startsWith("Program");
a.
0
c.
true
b.
1
d.
false
 

 14. 

The output of the following program is ____.

public class Test {
  public static void main(String[] args) {
     String location = "downtown";
     int j = location.indexOf('l');
     System.out.println("j = " + j);
  }
}
a.
j = -1
c.
j = 4
b.
j = 0
d.
j = 8
 

 15. 

What is the output of the following program?

public class Test {
  public static void main(String[] args) {
    String myString = "A class about strings";
    String myString2 = myString.substring(0, 13) + " main";
    System.out.println(myString2);
  }
}
a.
A class about strings
c.
A class main
b.
A class about main
d.
A class aboutmain
 

 16. 

Which is the correct declaration for an array of floats?
a.
float[] anArrayOfFloats;
c.
float anArrayOfFloats;
b.
[] float anArrayOfFloats;
d.
anArrayOfFloats float[];
 

 17. 

What symbol do you use next to a data type to indicate an array?
a.
( )
c.
{ }
b.
[ ]
d.
| |
 

 18. 

Which one of the following is a declaration of an int array?
a.
[ ] int a;
c.
a [int];
b.
integer [ ] a;
d.
int [ ] a;
 

 19. 

To allocate space for an array, you would use ____.
a.
new
c.
mem
b.
alloc
d.
spcalloc
 

 20. 

What is the index of 300?

   int [] a = {50, 75, 100, 150, 200, 300, 450, 510, 700};
a.
3
c.
5
b.
4
d.
6
 

 21. 

Each element of myArray has a value of ____.

    int [] myArray = new int[10];
a.
0
c.
-1
b.
null
d.
1
 

 22. 

What is the length of the following array?

    int[] a = {2, 4, 6, 8, 10};
a.
5
c.
7
b.
6
d.
10
 

 23. 

How many elements are there in the following array?

    boolean[] answers = { true, false, true, true, false };
a.
2
c.
5
b.
4
d.
6
 

 24. 

What is the output of the following program?

public class Test {
  public static void main(String [] args) {
    int[] a = {2, 4, 6, 8, 10, 12, 14};
    for(int i = 0; i < a.length; i++) {
      if (i > 5)
        a[i] *= 2;
    }
    printArray(a);
  }

  public static void printArray(int [] a) {
    for(int i=0; i < a.length; i++) {
      System.out.print(a[i] + "  ");
    }
  }
}
a.
2  4  6  8  10  24  28
c.
2  4  6  8  20  24  28
b.
2  4  6  8  10  12  28
d.
2  4  6  8  10  12  14
 

 25. 

What is the output of the following program?

public class ArrayDemo {
    public static void main(String[] args) {
        int[] anArray;            
        anArray = new int[10];     
        for (int i = 0; i < anArray.length; i++) {
            anArray[i] = i;
            System.out.print(anArray[i] + " ");
        }
        System.out.println();
    }
}
a.
0 1 2 3 4 5 6 7 8
c.
   1 2 3 4 5 6 7 8 9
b.
0 1 2 3 4 5 6 7 8 9
d.
0 1 2 3 4 5 6 7 8 9 10
 

 26. 

Which statement reserves enough computer memory for 7 Employee objects?
a.
Employee[] emp = new Employee;
c.
Employee emp = new Employee[7];
b.
Employee[7] emp = new Employee;
d.
Employee[ ] emp = new Employee[7];
 

 27. 

What is the output of the following program?

public class Test {
  public static void main(String [] args) {
    int [] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    int value = 9;
    boolean foundIt = false;
    for(int i=0; i < a.length; i++) {
      if (a[i] == value) {
        System.out.println(i);
        foundIt = true;
      }
    } 
    if (!foundIt)
      System.out.println("value not found");
  }
}
a.
value not found
c.
8
b.
-1
d.
9
 

 28. 

What is the output of the following program?

public class Test {
  public static void main(String[] args) {
     String [] a = {"This", "is", "a", "test"};
     for (int i = 0; i < a.length; i++) {
         if (a[i].length() > 2)
            System.out.print(a[i] + " ");
     }
  }
}
a.
This is a
c.
this test
b.
This is
d.
This test
 

 29. 

The output of the following program is ____.

public class Test {
  public static void main(String [] args) {
    int[] a = new int[7];
    for(int i=0; i < 5; i++) {
      a[i] = i % 2;
    }
    printArray(a);
  }

  public static void printArray(int [] a) {
    for(int i=0; i < a.length; i++) {
      System.out.print(a[i] + "  ");
    }
  }
}
a.
0  1  0  1  0  1  1
c.
1  0  1  0  0  0  0
b.
0  1  0  1  0  0  0
d.
0  1  0  1  0  1  1
 

 30. 

Which one of the following is the correct declaration for a String array?
a.
String anArrayOfStrings;
c.
String[] anArrayOfStrings;
b.
anArrayOfStrings String[];
d.
string[] anArrayOfStrings;
 

Completion
Complete each statement.
 

 31. 

A(n) ____________________ is a variable that holds a memory address.
 

 

 32. 

The char data type is a(n) ____________________ data type.
 

 

Matching
 
 
Match each item with a statement below.
a.
length property
f.
Double
b.
immutable
g.
capacity
c.
concatenation
h.
anonymous
d.
StringBuffer
i.
String
e.
Character
 

 33. 

Objects that cannot be changed.
 

 34. 

A class that defines methods that can manipulate or inspect single-character data.
 

 35. 

A wrapper class that is imported into programs automatically.
 

 36. 

A class for working with fixed-string data.
 

 37. 

Joining a simple variable to a String, creating a longer String.
 



 
Check Your Work     Start Over