COMPSCI 121 Study Guide - Midterm Guide: Earthbound

96 views8 pages
26 Oct 2018
Professor
1
Computer Science 121
Midterm Exam * Spring 2014
Be sure to show all your work. Remember: exam is closed book.
Cellphones, computers, calculators, ear buds of any sort, slide rules, notes, talking, etc., are not permitted. Keep all of your scratch work on the
exam sheets. Feel free to use the opposite blank side for your answer.
WRITE LEGIBLY! IF WE CAN’T READ IT, YOU WON’T GET CREDIT.
Name:__________________________________
Student Number: _______________________
-------------------- do not write below this line ------------------------
Score:
(30) Write a complete program in a single class called StarString, which does the following. First it reads in a line of text from the keyboard.
Then it prints to the screen the entered line of text, with the following special condition. Every third character should be replaced with a star
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in
2
symbol ‘*’.
For example, if the line is
Abcdefghijklmn your code should print *bc*ef*hi*kl*n
And if it’s
The dog barks your code should print *he*do* b*rk*
Thus, in any String, the characters at positions 0,3,6, and so forth should be replaced with *.
import java.util.Scanner; //import Scanner to be used later
//Define class
public class StarString
{
//set up main
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in); //set up scanner
String input = scan.nextLine(); //use next line method and assign it to a String
//for loop to cycle through String and print out every third character
for(int i = 0; i < input.length(); i++)
{
if(i%3 == 0)
{
//use print, not print line since you want to retain the format of the string
System.out.print("*");
}
else
System.out.print(input.charAt(i));
}
System.out.println(); //to move the cursor to the next line
}
2. (5) What does this statement print? Put a circle around your answer.
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in
3
System.out.println((110 % 20) / ((784 % 100) / 10));
110 % 20 = 10 784 % 100 = 84 84 / 10 = 8 10 / 8 = 1 <- Final Answer
3. (5) Write a for loop that prints in a column all of the even numbers from 150 to 66 inclusive except for 132 and 104.
for(int i =150; i<=66; i++)
{
//Need to use ands since it cannot be either of those numbers and has to be even
if(i != 132 && i != 104 && i % 2 ==0)
{
System.out.println(i);
}
}
4. (5) What do these statements print? Put a circle around your answer (note: (int)‘A’ = 65)
int n = 70;
if (‘A’ > n) System.out.println(‘A’*10); else System.out.println(‘A’/10);
Since 70 > 65, the else statement kicks off. We get 65 / 10 = 6 <- Final answer
5. (30) Consider the Fence class below:
public class Fence{
private double length; // in feet
private int height; // in feet
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 8 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Cellphones, computers, calculators, ear buds of any sort, slide rules, notes, talking, etc. , are not permitted. Keep all of your scratch work on the exam sheets. Feel free to use the opposite blank side for your answer. If we can"t read it, you won"t get credit. ------------------- do not write below this line ------------------------ Score: (30) write a complete program in a single class called starstring, which does the following. First it reads in a line of text from the keyboard. Then it prints to the screen the entered line of text, with the following special condition. Every third character should be replaced with a star. The dog barks your code should print *he*do* b*rk* symbol *". Thus, in any string, the characters at positions 0,3,6, and so forth should be replaced with *. import java. util. scanner; //import scanner to be used later. Scanner scan = new scanner(system. in); //set up scanner.