JMU JMU - Department of Computer Science
Help Tools
Written Portion of Exam 2 - Sample


Answer all of the following questions. This is an "open book" exam, but you must work entirely on your own.
  1. (5 points) All of the questions below assume the prior existence of the declaration statements int january; and int[] month;. Given that, provide the best answer to each.
    (1) _____ In the Java statement january = new month[12];, new is:
    1. An array
    2. A method
    3. An object
    4. An operator
    5. A parameter
    6. A variable
    (2) _____ In the Java statement january = month[0];, january is:
    1. An array
    2. A method
    3. An object
    4. An operator
    5. A parameter
    6. A variable
    (3) _____ In the Java statement january = month[0];, = is:
    1. An array
    2. A method
    3. An object
    4. An operator
    5. A parameter
    6. A variable
    (4) _____ In the Java statement january = month[0];, month is:
    1. An array
    2. A constant
    3. A method
    4. An object
    5. An operator
    6. A parameter
    (5) _____ In the Java statement january = month[0];, [] is:
    1. An array
    2. A method
    3. An object
    4. An operator
    5. A parameter
    6. A variable
  2. (10 points) Consider the following fragment (written in Java):
            public static int vowelCounter(String s) {
    	   char letter;
    	   int n;
    	   int vowels;
               String lc;
    	
    	   lc = s.toLowerCase();
               n = s.length();
    
    	   for (int i=0; i<n; i++) {
    	     letter = lc.charAt(i);
    	     if ((letter == 'a') || (letter == 'e') || (letter == 'i') ||
                     (letter == 'o') || (letter == 'u')) {
                 vowels++;             
                }
                return vowels;
             }
          

    Identify the best description of each of the following as it is used (explicitly or implicitly) in this fragment. You may use a description more than once.

    _____ s
    _____ toLowerCase()
    _____ charAt()
    _____ for
    _____ ||
    _____ <
    _____ .
    _____ String lc;
    _____ null
    _____ int
    1. A return type
    2. An indeterminate loop
    3. The name of a mutable object
    4. A declaration statement
    5. The name of an attribute
    6. A relational operator
    7. A determinate loop
    8. The name of an immutable object
    9. The membership operator
    10. The name of a static method with no parameters
    11. The name of a non-static method with no parameters
    12. A logical operator
    13. A constructor
    14. The name of a static method with one parameter
    15. The name of a non-static method with one parameter
    16. The name of a class
    17. An initialization statement
    18. The value of lc immediately after it is declared
  3. (10 points) Indicate whether each of the following fragments will generate a compile-time error (C), won't generate a compile-time error but will generate a run-time error (R), won't generate a compile-time or run-time error but contains a logic error (L), or none of the above (N) (treating the fragment in isolation but assuming it is properly included in a method in a class). Work slowly and be careful.
    	String dept;
    	dept.toLowerCase();
          


    	String dept;
    	dept = new String("cs");
    	dept.toUpperCase();
          


    	String dept;
    	dept = new String("cs");
    	dept = dept.toUpperCase();
          


    	String dept;
    	String code;
    	dept = new String("cs");
    	code = dept.toUpperCase();
          


    	String course;
    	course = new String("CS") + new String("149");
          


  4. (20 points) Indicate whether each of the following fragments will generate a compile-time error (C), won't generate a compile-time error but will generate a run-time error (R), won't generate a compile-time or run-time error but contains a logic error (L), or none of the above (N) (treating the fragment in isolation but assuming it is properly included in a method in a class). Work slowly and be careful.
    	int[] scores;
    	scores = 5;
          


    	int[] scores;
    	scores[0] = 5;
          


    	int[] scores;
    	scores = new int[10];
    	scores[10] = 5;
          


    	int[] scores;
    	scores = new int[5];
    	int n;
    	n = scores.length;
          


    	int[] scores;
    	scores = new int[5];
    	scores.length = 10;
          


    	int[] scores;
    	scores = new int[10];
    	scores = new int[20];
          


    	int[] scores;
    	scores = new int[10];
    	scores[0] = 100;
          


    	int[] scores;
    	scores = new int[10];
    	scores[0] = 100.0;
          


    	int[] scores;
    	scores = new int[10];
    	for (int i=0; i<scores.length; i++) {
    	  scores[0] = 0.0;
    	}
          


    	int[] scores;
    	scores = new int[10];
    	for (int i=0; i>0; i++) {
    	  scores[i] = 0.0;
    	}
          


  5. (5 points) Answer each of the following questions.
    In the following fragment:
    	String star;
    	String costar;
    
    	star = new String("Fred");
    	costar = new String("Wilma");
    	if (star == costar) {
    	  System.out.println("Here");
    	}
          

    the expression star == costar will evaluate to false. Explain why.



    In the following fragment:
    	String star;
    	String costar;
    
    	star = new String("Fred");
    	costar = new String("Fred");
    	if (star == costar) {
    	  System.out.println("Here");
    	}
          

    the expression star == costar will evaluate to false. Explain why.



    In the following fragment:
    	String star;
    	String costar;
    
    	star = new String("Fred");
    	costar = new String("Fred");
    	if (star.equals(costar)) {
    	  System.out.println("Here");
    	}
          

    the expression star.equals(costar) will evaluate to true. Explain why.



    In the following fragment:
    	int[] week1 = {0, 1, 2, 3, 4, 5, 6, 7};
    	int[] week2 = {0, 1, 2, 3, 4, 5, 6, 7};
    
    	if (week1 == week2) {
    	  System.out.println("Here");
    	}
          

    the expression week1 == week2 will evaluate to false. Explain why.



    In the following fragment:
             int[] week1 = {0, 1, 2, 3, 4, 5, 6, 7};
             int[] week2 = {0, 1, 2, 3, 4, 5, 6, 7};
             boolean equal = true;
    
             for (int i=0; i<week1.length; i++) {
               if (week1[i] != week2[i]) {
                 equal = false;
               }
             }
          

    the variable equal will contain the value true after the loop terminates. Explain why.



Copyright 2020