COMPSCI 121 Study Guide - Midterm Guide: Substring, Yusef Lateef

89 views9 pages
26 Oct 2018
Professor
Midterm Exam 1 Spring 2016
Question 1. 25 points total
1. Write a code fragment that prints in a column all of the multiples of 9 from 1 to 80. Thus
your output should be:
9
18
27
36
45
54
63
72
Your code must contain a for loop statement. Enter your answer below.
( 7 pts.)
Note that there are three versions of this problem
//Q1 a
for(int i=4;i<=40;i+=4){
System.out.println(i);
}
for(int i=7;i<=60;i+=7){
System.out.println(i);
}
for(int i=9;i<=80;i+=9){
System.out.println(i);
}
---------------------------------------------------------------------------
Unlock document

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

Already have an account? Log in
2. Suppose the variable str is some String.
Write a for loop that prints the characters in str in reverse order. For example, if str was:
Yusef Lateef
your code should print:
feetaL fesuY
Enter your answer below.
( 8 pts.)
--------------------------------------------------------------------------------------------------------
for(int i=str.length()-1;i>=0;i--){
System.out.print(str.charAt(i));
}
3. Suppose an int variable n has been declared and is initialized to a value >0. Write a loop
that prints the word "BLACK" and then "WHITE" on different lines n times. So, the
output:
BLACK
WHITE
Is repeated n times.
Furthermore, the line numbers for the sequence are printed after each word, starting at 1.
For example, your code would produce the following output for n=1:
BLACK 1
WHITE 2
Another example: your code would produce the following output for n=3:
BLACK 1
WHITE 2
BLACK 3
WHITE 4
BLACK 5
WHITE 6
Unlock document

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

Already have an account? Log in
Enter your answer below.
( 10 pts.)
//Q1 c
int k=1;
while(k <= (2*n)){
System.out.println("BLACK "+k);
System.out.println("WHITE "+(k+1));
k+=2;
}
OR
int j = 1, k=1;
while(j <= n){
System.out.println("BLACK "+k);
System.out.println("WHITE "+(k+1));
j++;
k+=2;
}
Question 2. 25 points total
1. Write the method isDivisible which takes two int parameters called num1 and num2,
and returns true if num1 is evenly divisible by num2, otherwise it returns false. Assume
both parameters are greater than 0.
For example, this call isDivisible(115, 23); returns true, while this call
isDivisible(55, 18); returns false
Enter your answer below.
( 7 pts.)
public boolean isDivisible(int num1, int num2) {
return (num1%num2==0);
}
Unlock document

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

Already have an account? Log in

Document Summary

25 points total: write a code fragment that prints in a column all of the multiples of 9 from 1 to 80. Your code must contain a for loop statement. Note that there are three versions of this problem. System. out. println(i): suppose the variable str is some string. Write a for loop that prints the characters in str in reverse order. Yusef lateef your code should print: feetal fesuy. Enter your answer below. ( 8 pts. ) for(int i=str. length()-1;i>=0;i--){ System. out. print(str. charat(i)): suppose an int variable n has been declared and is initialized to a value >0. Write a loop that prints the word black and then white on different lines n times. Furthermore, the line numbers for the sequence are printed after each word, starting at 1. For example, your code would produce the following output for n=1: Another example: your code would produce the following output for n=3: Or int j = 1, k=1; while(j <= n){

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers

Related Documents

Related Questions