Here are five practice problems for the midterm exam on October 7, 2005. You will be able to pick up the real midterm exam after Friday at 8pm on the 7th from the bin beside my office in Olin 1241.
The midterm exam itself will be worth 100 points (the equivalent of 2 HWs) and will consist of 4 problems. It will have a time limit of 3 hours.
The midterm exam is closed-book and is to be taken without the help of outside resources or the use of a computer. However, you may use a double-sided page of notes that you have prepared beforehand. You should hand in the notes with your exam.
The exam is due to me by midnight on Sunday, Oct 9th, 2005. Simply slide it under my door.
Write a java program that prompts the user to type two words.
The program should then print out the two words in reverse order
and with their letters reversed. For example, if the input words were
The output should be
computer
science
ecneics
retupmoc
You don't have to worry about the lines
starting your program -- you can simply start with
class CS5App
{
There is no need to write out the full signature of the void main()
{
// put your code here
}
main
method. (It's given below in the example programs.)
You may assume the user will input two words and hit return after each.
As a result, you may use either nextWord() or nextLine()
to take in the two words.
Recall that if s is a String, then
returns the s.charAt(i)
ith character of the String s.
Also,
returns the length of the s.length()
String s.
Write a java program that continues to prompt the user to input
an integer and then reads in an integer. The program should only
stop taking input when the user types a -1.
After the program stops taking input, it should print two things:
Here is an example run of the program:
Please type an integer (-1 to stop): 1
Please type an integer (-1 to stop): 2
Please type an integer (-1 to stop): 3
Please type an integer (-1 to stop): 4
Please type an integer (-1 to stop): 25
Please type an integer (-1 to stop): -1
There were 5 integers entered.
Their average is 7.0
What will this program print if a user types in the string
when prompted for input?
fcllqvfctc
Here is the code:
public static void main(String[] s)
{
H.out.println("Type a string at the prompt: ");
String s = H.in.nextWord();
H.out.println();
for (int i=0 ; i < s.length() ; ++i)
{
char c = mixedUpChar( s.charAt(i) );
H.out.print( c );
}
H.out.println();
}
public static char mixedUpChar(char c)
{
char result = c;
if (c <= 'm')
{
result += 2;
}
if (result >= 'm')
{
result -= 2;
}
return result;
}
What does this program print if a user types in the string
when prompted for input?
disappearing
Here is the code:
public static void main(String[] s)
{
H.out.print("Type a string at the prompt: ");
String s = H.in.nextWord();
H.out.println();
for (int i=0 ; i < s.length() ; ++i)
{
for (int j=i ; j < s.length() ; ++j)
{
H.out.print( s.charAt(j) );
}
H.out.println();
}
}
(A) Write a java program that uses one or more for loops to print out the first 10 terms
of this sequence:
2 4 8 16 32
(B) Write a java program that uses one or more for loops to print out the first 10 terms
of this sequence:
Notice that the basic terms are the same as in part (A), but here there are two of the
second term (that is, two fours), three of the third term (that is, three eights), etc.
2 44 888 16161616 3232323232