Spring 2015, Wednesday, 2/11/15

advertisement
CS 110G Exam 1
2/11/2015
Page 1
Computer Science I G
Spring 2015, Wednesday, 2/11/15
100 points
Name _________Key_______________
1. Fill in the remaining 4 boxes that identify the primary components of a computer system. Use general terms
for the components.
[12 pts]
Data flow
CPU
Input
Output
Primary Memory
Storage
The smallest addressable unit in primary memory is a _byte____ and each is composed of _8___ bits, and
bits can hold the values __0/false____ and __1/true__ .
2. True/False on Java compiling and debugging.
[10 pts]
__T___ The class name of the program and the source file name with a .java extension must be the same.
__ T ___ The Java compiler creates the .class bytecodes from the .java file.
__ F ___ The Java Virtual Machine executes the program only from the .java source file.
__ T ___ Java software is compile once, run anywhere.
__ T ___ A Java application must have a main method in it.
__ F ___ A missing semicolon is an example of a runtime error.
__ F ___ Typing “hello” interactively when executing a nextInt() method is an example of a syntax error.
__ F ___ Resolving all syntax errors guarantees a correctly working program.
__ F ___ A statement expressing a calculation at the beginning of the program is executed every time a
variable in the calculation changes just like in a spreadsheet.
__ F ___ The compiler uses comments to help generate correct code.
2/11/2015
CS 110G Exam 1
Page 2
3. True/false on variables and types.
[10 pts]
__ F __A variable can tracks all values it takes on during the execution of the program.
__ F __ The variable total is the same as the variable Total; capitalization is immaterial.
__ T __ A variable must be declared with its type before it can be used.
__ F __ “95”, 95, and 95.0 are understood by Java to be the same value and can be used interchangeably.
__ T __ A variable’s type remains fixed throughout the program, but its value can change.
__ F __ A double value can be assigned to an int variable type without loss of information.
__ F __ When a double is typecast as an int, it automatically rounds up or down.
__ T __ A char is encoded inside Java and computer as a number.
__ F __ The String type is treated as a primitive just like int or double.
__ T __ Every variable’s value can be automatically converted to a String for concatenation or printing
purposes in Java.
4. 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 expressing the answer in the correct type notation is also worth
one point.
[20 pts]
Assume the following variables and initial values:
int a = 12;
int b = 8;
double c = 6.0;
double d = 1.5;
String s = “The answer is:”;
___8_____ = a - b / 2
___14____ = s.length( )
___3.0___ = c / d – 1.0
___’e’___ = s.charAt(b)
___-2.0__ = a – b – c
“The answer is:128” = s + a + b
___12____ = a % b * 3
___ false __ = a < c * d
___1.5___ = 6 / c + 0.5
___false_ = b<=a && d>=2.0
5. Show the output for the following code segments.
[5 pts]
System.out.print(“abcd“);
System.out.println(“efgh“);
System.out.print (“ijkl\nmop\n“);
System.out.println(“rstu“ +
“wxyz“);
abcdefgh
ijkl
mop
rstuwxyz
CS 110G Exam 1
2/11/2015
Page 3
6. 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. Comments contain hints (nice, huh?).
[13 pts]
import java.util.Scanner;
public class largest
{
public static void ___main__ (String [] args) //start, main, Main, Start
{
double __n_____; // input variables
_double__ ____m_____;
Scanner keyboard = new __Scanner_ (System.in);
System.out.println(“Enter two numbers: “);
first
= keyboard.___nextDouble_____ ();
// constructor is same as class
//important Scanner methods next
second = keyboard.___ nextDouble ___ ();
__if__ (___n _____ > ____ m _____) // test, if, check, throw, catch
{// choices above here drive answers in printlns
System.out.println (“The ____ n _______ number is the largest”);
} _else__ {//then otherwise default else return
System.out.println (“The ______ m _____ number is the largest”);
}
}
}
7. Explain or give the UNIX or vi commands/keystrokes to do the following tasks. A single command should
suffice.
[12 pts]
a. Display all filenames that are in your current working directory. ____ls______
b. Display the contents of dnaGen.java file found ____cat dnaGen.java____________
c. Delete the temp file in your current directory __rm temp_____
d. Create a subdirectory in your current directory called projects. ____mkdir projects___________
e. Move all java type files in your current directory to this subdirectory projects __mv *.java projects ___
(You can use a * for a wildcard for general root file names in this case.)
f.
Change your current working directory to its parent directory (up one level) _____cd .. _________
g. Compile the source file exam.java ____javac exam.java__________
h. Run the Java program exam ______java exam______________________
i.
True/false on Unix
___T___The ctrl-D key combination designates end of input in Unix.
___ T ___The ctrl-C key combination stops and cancels a program in Unix.
___ T ___All directories, disk drives, file storage structures are unified into one directory tree.
___ F ___Text files’ end-of-line coding is consistent between Windows/DOS and Unix.
2/11/2015
CS 110G Exam 1
Page 4
8. Write a complete Java program (use #7 as a guide for structure) to input a DNA sequence. Search if the
sequence has a start codon “ATG” using the indexOf() method. If so, reset the sequence with substring() so
that you can output the sequence starting with ATG discarding the letters preceding ATG. Then search this
new string for “TAA” as a stop codon. Print a message whether you found it and if its position is divisible
by 3. That’s all. (If it is divisible by 3 it’s really a stop codon.) Be sure the output is well labeled so a casual
user understands the results.
Example:
 Input: CGATGAGGTAGDTAGTAAATAAA
 Output:
Start found, sequence is ATGAGGTAGDTAGTAAATAAA
Stop found at position 11
It is not aligned.
[18 pts]
import java.util.Scanner;
public class largest
{
public static void main (String [] args)
{
String seq;
Scanner keyboard = new Scanner (System.in);
System.out.println(“Enter a DNA sequence: “);
seq = keyboard.next();
int start = seq.indexOf(“ATG”);
if (start != -1) {
seq = seq.substring(start);
System.out.println(“Start found, sequence is “+seq);
int stop = seq.indexOf(“TAA”);
System.out.println(“Stop found at position “+stop);
if(stop%3==0){
System.out.println(“It is aligned!”);
} else {
System.out.println(“It is not aligned!”);
}
} else {
System.out.println(“**No start codon found.**”);
}
}
}
Download