CPSC 124, Spring 2021: Sample Answers to Test #1

These are sample answers only. In many cases, answers that are less
detailed than the ones given here can still receive full credit.

Question 1. What is machine language, and how does it relate to high-level programming language?

Answer. Machine language is a programming language in which instructions are encoded as binary numbers that can be executed directly by the hardware of a computer. A high-level programming language such as Java must be translated into machine language before it can be executed by a computer.

Question 2. What is done by the following variable declaration statement? (You should give a clear and complete answer.)

   double x,y,z;

Answer. The statement creates three variables named x, y, and z. The computer assigns these names to memory locations, so that those locations can be referred to by name in the program. These variables are of type double, which means that they can hold real numbers (with decimal points).

Question 3. Write a loop, using correct Java code, that will print out all the integers from 1 up to 1000, with one integer per line. Declare any variables that you use.

Answer.

      int i;
      for (i = 1; i <= 1000; i++) {
          System.out.println(i);
      }

Question 4. Suppose that temp is a variable of type int, and that the value of temp is the current temperature. Write an if..else if... statement that will print out one of the three messages: "It's hot" when the temperature is greater than 85, "It's nice" when the temperature is in the range 60 to 84, and "It's cold" when the temperature is less than 60.

Answer.

      if (temp > 85) {
          System.out.println("It's hot");
      }
      else if (temp < 60) {
          System.out.println("It's cold");
      }
      else {
          System.out.println("It's nice");
      }

Another possible answer:

      if (temp > 85) {
          System.out.println("It's hot");
      }
      else if (temp > 59) {
          System.out.println("It's nice");
      }
      else {
          System.out.println("It's cold");
      }

Question 5. Suppose that the variable str is of type String. Write a code segment that will count the number of times that the character 'e' occurs in the string str, and print out the answer. You should declare any additional variables that are used in your code. (Hint: Go through the string character-by-character, and compare each character to 'e'.)

Answer.

      int i, countE;
      countE = 0;
      for (i = 0; i < str.length(); i++) {
          if ( str.charAt(i) == 'e' ) {
              countE++;
          }
      }
      System.out.println("Number of e's is " + countE);

Question 6. Show the exact output produced by the following code segment:

int n, x, y;
x = 1;
y = 5;
while ( x <= y ) {
    x = x*3;
    y = y*2;
    n = y / x;
    System.out.println(n + "," + x + "," + y);
}
System.out.println("Done");

Answer.

      3,3,10
      2,9,20
      1,27,40
      0,81,80
      Done

(Note: The values of x and y before the loop are 1 and 5. The first time through the loop, they are changed to 3 and 10, and n is computed as the integer quotient 10/3, which is 3, since the integer quotient discards the remainder. So the output is 3,3,10, with a new line at the end. Since x is still less than y, the loop continues; this time, x and y become 9 and 20, and n = 20/9 = 2, so the output is 2,9,20. Again, x is less than y, so the loop continues; x and y become 27 and 40, and n = 40/27 = 1, so the output is 1,27,40. The value of x is still less than y, so the loop continues; this time, x and y become 81 and 80, and n = 80/81 = 0, so the output is 0,81,80. Finally, x is greater than y, the loop ends and the final output, Done, is printed. Note that the line 0,81,80 is output even though x is greater than y at that point, because the test for continuing the loop is only done at the start of the loop.)

Question 7. Show the exact output produced by the following code segment:

       int i,j;
       for ( i = 6; i >= 0; i-- ) {
           for ( j = 1; j <= i; j++ ) {
               System.out.print(j);
           }
           System.out.println();
       }

Answer.

      123456
      12345
      1234
      123
      12
      1

Question 8. Describe, in words, the picture that will be produced by the following code segment in a JavaFX program.

      int y;
      y = 40;
      
      g.setFill(Color.BLUE);
      g.fillRect(0,0,800,600);
      
      for (int i = 0; i < 13; i++) {
          if ( i % 2 == 0 ) {
              g.setFill(Color.RED);
          }
          else {
              g.setFill(Color.WHITE);
          }
          g.fillRect( 40, y, 720, 40);
          y = y + 40;
      }

Answer. This draws 13 rectangles, on a blue background. The rectangles are 720 pixels wide and 40 pixels tall, and are stacked up vertically with no space between them. The colors of the rectangles alternate between red and white, starting with red. The rectangles fill the window, except for a 40-pixel blue border around them.

Question 9. We have looked at three aspects of computer programming: syntax, semantics, and style. Explain what each of these means and why it is important, and for each one, give a specific example from Java programming. This is an essay question. Answer in full sentences and paragraphs.

Answer. Syntax refers to the "grammar" of a language. That is, syntax rules specify what is legal in a program. A program that is syntactically correct can be compiled, but a program with syntax errors will be rejected by the computer. An example of a syntax rule in Java is the fact that a variable must be declared before it can be used.

Semantics refers to the meaning of a language. For a programming language, semantic rules determine what the program does when it is run. A semantic error means that the program runs, but does not behave correctly. A compiler cannot find semantic errors. An example of semantics in Java is the fact than an assignment statement computes the value of the expression on the right of the equals sign and stores it in the variable on the left of the equals sign.

Style refers to the rules of good programming practice. Style is ignored by the computer and has no effect on what the program does when it is run. It is there for human readers of the program, to make it easier to understand. An example of a style rule is that every variable declaration needs a comment to explain its purpose in the program.

Question 10. A central concept in computer science is algorithm. Discuss the concept of algorithm. What is meant by an algorithm? What do algorithms have to do with computer programs? Where do algorithms come from? This is an essay question. Answer in full sentences and paragraphs.

Answer. An algorithm is an unambiguous step-by-step procedure for performing some task. Usually, it is also assumed that the procedure is guaranteed to finish after a finite number of steps. Algorithms are the central idea in computer science. Algorithms are similar to program, but are more abstract. You don't need a computer program to have an algorithm. An actual computer program is a specific implementation of an algorithm. The algorithm is the idea behind the program, and it can be developed independently of a programming language.

When trying to write a program to perform some task, a programmer must develop an algorithm for that task, and then implement that algorithm in a syntactically correct computer program. During the development, it is often helpful to write the algorithm in pseudocode — informal language used to specify algorithms without the rigorous syntax of a a programming language. The algorithm can be written first in broad outline form, then gradually refined by adding detail, a process known as stepwise refinement of the algorithm. When the algorithm is complete, it can be translated into actual computer code in some programming language.