2/8/2012 CS 110 Exam 1 Page 4

Computer Science I

Spring 2012, Wednesday, 2/08/12

100 points

Name ______

1.  Components of a computer hardware system.

[8 pts]

What is the part of the computer system that performs the calculations and control called?
______

Name two hardware input components: ______

Name two hardware output components: ______

Comparing primary storage and secondary storage, which is closest to the part above that calculates and controls? ______And which has the larger capacity? ______

2.  A bit of storage can hold what values? ______and ______. There are ______bits combined into smallest addressable units in primary memory called a ______, but often we group ____ or _____ of these addressable units to hold an integer or double data type.

[6 pts]

3.  True/False on Java compiling and debugging.

[11 pts]

______The class name of the program and the source file name with a .java extension must be the same.

______The Java compiler creates the .java source from the .class file.

______The computer executes the program only from the .class file.

______The .class file and the .java file has the same root file name.

______A Java application must have a main method in it.

______A Java applet must have a main method in it.

______A missing semicolon is an example of a syntax error.

______Typing “hello” interactively when using a nextInt() method is an example of a logic error.

______Resolving all syntax errors guarantees a correctly working program.

______A statement expressing a calculation at the beginning of the program is executed every time a variable in the calculation changes like in a spreadsheet.

______The compiler uses comments to help generate correct code.

4.  True/false on variables and types.

[10 pts]

_____A variable retains all previously held values.

_____ The variable total is different from the variable Total.

_____ A variable must be declared with its type before it is used.

_____ “15”, 15, 15.0, and ‘1’ ‘5’ are understood by Java to be the same value.

_____ A variable’s type remains fixed throughout the program.

_____ A double value can be assigned to an int variable type without loss of information.

_____ When a double is typecast as an int, it automatically rounds up or down.

_____ A char is encoded inside Java and computer as a number.

_____ The String type is treated as a primitive just like int or double.

_____ Every variable’s value can be automatically converted to a String for concatenation or printing purposes.

5.  Evaluate the following expressions. Be sure to use the correct syntactic notation for each of the results. E.g., appropriate inclusion of decimal points, single quotes or double quotes to represent the type of the result. The correct value is worth a point and the correct type constant notation is also worth one point.

[20 pts]

Assume the following variables and initial values:

int a = 12;

int b = 8;

double c = -2.0;

double d = 1.5;

String s = “good luck today”;

2/8/2012 CS 110 Exam 1 Page 4

______= a - b / 2

______= a / b + 4

______= a – b – c

______= b % a * 3

______= 6 / c + 1

______= s.length( )

______= s.charAt(b)

______= a + b + s

______= a – b < c * d

______= a<=b||c=2.0d>=1.0

2/8/2012 CS 110 Exam 1 Page 4

6.  Show the output for the following code segments.

[7 pts]

System.out.print(“abcd“);

System.out.println(“efgh”);

System.out.print (“ijkl\nmop\n”);

System.out.println(“rstu”);

System.out.println(“What happens”+

“\there?”)

7.  Fill in the blanks to complete a Java program to input two double type numbers and prints which one is the largest of the two values input.

[13 pts]

import java.util.Scanner;

public class largest

{

public static void ______(String [] args)

{

double ______;

______;

Scanner keyboard = new ______(System.in);

System.out.println(“Enter two numbers: “);

first = keyboard.______();

second = keyboard.______();

______(______< ______)

{

System.out.println (“The ______number is the largest”);

} ______{

System.out.println (“The ______number is the largest”);

}

}

}

8.  What would be drawn if the following graphics calls were made? Assume the canvas is at least 200x200 pixels. Sketch relative sizes. Relative placement is most important but exact dimensions are not required.

[10 pts]

canvas.drawRect(0,0,100,100);

canvas.fillRect(25,0,50,30);

canvas.drawOval(50,50,25,50);

canvas.drawLine(0,100,50,75);

9.  Write a complete Java program (use #7 as a guide for structure) to input two words. Print the two words in opposite order as entered separated by a hyphen (“-“). Print the longest of the two words. And print the “lesser” of the two words, lexicographically. As the writer of the program, you don’t know, of course, what the user will put in.

Examples:

·  Input of pear orange à “orange-pear” “orange is the longest word”, “pear” is the lesser word”

·  gerbil cat à “cat-gerbil”, “gerbil is the longest word”, “cat is the lesser words”

Some hints:

·  The keyboard.next() method returns the next word in the input stream.

·  Remember about comparing strings.

·  Remember about getting the length of strings.

·  Be sure the output is well labeled so a casual user understands the results.

[15 pts]