0% found this document useful (0 votes)
9 views

04a. WS Rev U1a Java Basics W Methods SOL

Uploaded by

freeve4handcgame
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

04a. WS Rev U1a Java Basics W Methods SOL

Uploaded by

freeve4handcgame
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

AP CSA

Name______________________
Review for Test U1a: Basic Java (Conditionals, Loops, Methods, Strings, DeMorgan's)
For #1-2, evaluate the expression.
// Exercise #4: What's the output?
1.) 10 + 28 % 6 - (7 - 10 / 4)
int a = 21, b = 10, c = 32;
2.) 36 / 5 / 2.0 - (4.2 + 7.8) - 22 /
5 if (c > a)
{
// Exercise #3: What's the output? c = a - 4;
}
int a = 21, b = 11, c = 8; else
{
if (c > a) if ( b < a )
{ {
c = a - 4; c = c - 2;
} }
else else
{ {
if ( b < a ) c = a + 22;
{ }
c = c - 2; }
} if (c <= a)
else {
{ b = b % 4;
c = a + 22; }
} System.out.print( a + " " + b + " " + c);
}
if (c <= a)
{
b = b % 4;
}
System.out.print( a + " " + b + " " + c);

// Exercise #5: What's the output? // Exercise #6: What's the output?
int x = 35, y = 21, z = 14;
if (y * 2 < x) int x = 21, y = 15, z = 14;
{ if (y * 2 < x)
z = x + 23; {
} z = x + 23;
else if ( y > z ) }
{ else if ( y < z )
x /= 4; {
if ( x > y ) x /= 4;
{ }
x += 11; else
} {
else if ( z > y ) if ( z + 1 <= y )
{ {
x -= 3; z = 25 - z;
} }
} if ( x >= y )
else {
{ z -= 6;
z = 19 - z; }
} }
if (z <= x) if (z <= x)
{ {
z += 3; x += 3;
1
} }
System.out.print( x + " " + y + " " + z); System.out.print( x + " " + y + " " + z);
7.) What's the output? 8.) What’s the output?

for( int x = 10; x < 41; x += 2 ) int result = 200;


{ for( int j = 38; j > 22; j = j
if (x % 3 == 0)
System.out.println(x); - 4 )
} { result -= j; }
System.out.println( result );
9.) What’s the output? 10.) What’s the output?

int n = 3, answer = 1; int w = 14;


while( n < 7 ) while( w < 26 )
{ {
answer *= n;
n++; w += 3;
} System.out.print( w + " " );
System.out.print(answer); }

11.) What’s the output? 12.) What’s the output?

String s = ""; int k = 18;


for ( int k = 5; k < 10; k++ ) String s = "";
{ while(k > 10){
s = s + "_" + k; s = k + " " + s;
} k -= 2;
System.out.print(s); }
System.out.print(s);

Do #21-31 (required). You may do #13-20, 24.) Write the code to generate a random
but those problems are optional. integer from 762 to 819.

For #21-28, identify the type of answer you 25.) Write the code to generate a random
get (integer or decimal), and state the range of
integer from -18 to -7.
values. (Do not use interval notation.)

13.) 8 + 42 * Math.random() 26.) Write the code to generate a random


integer from -32 to 50.
14.) (int) (33 + Math.random() * 44)
15.) (int) (Math.random() * 180) - 501 27.) Fill in the blank so that the body of the "if"
16.) 14 * (int) ( Math.random() + 26 ) statement is executed 26% of the time.
17.) (int) ( Math.random() + 42 ) * 5 - 10
if ( ________________________ )
18.) (int) ( Math.random() ) * 17 + 63 {
19.) (int) ( Math.random() * 8 ) * 11 // body of the if statement
20.) (int) ( 3 * Math.random() ) * 7 + 4 }

For #36-39, negate the following


21.) How do you generate a random decimal
expressions.
between 6 and 14, not including 14?

28.) (x > y) && (y > z)


22.) How do you generate a random decimal
between 11 and 23, not including 23?
29.) (x == y) || (x <= z)
23.) Write the code to generate a random 30.) (x / 2 == 13) || b || (z * 3 == 96)
integer from 6 to 29.

2
31.) (z < x) && (z > y || x >= y)

For #32-50, given the Strings below, evaluate each of the expressions.

String one = "buttercreamvanilla"; String two = "realcupcakewars";

two.length() // one.indexOf("x") // #42


#32
two.indexOf("a") // #43
one.charAt(0) //
#33 two.indexOf("cake") // #44
one.charAt(5) //
#34 one.substring(0, 3).equals(two.substring(7, 10))
//#45
one.charAt(one.length()-3); //
#35 "k".compareTo("b") // #46

one.substring(2,5) // "cherry".compareTo("chip") // #47


#36
"paradise".compareTo("pardon")
one.substring(10) // // #48
#37
"dog".compareTo("DOA") // #49
two.substring(5) // #38
two.replaceAll("a","x") // #50
two.substring(2,8) //
#39

one.indexOf("nilla") ); // #40

one.indexOf('a') // #41

Part 2: Free-Response, Java Basics:


51.) Write a program to print out the following. 52.) Write a program to print out the
Use loops to print out one number at a time. following. Use loops to print out one
-3 4 11 18 25 number at a time.

public class MyProg { 44 32 20 8 -4 -16


public static void main ( String[] args ) {
// your code goes here
}
}

53.) Use nested loops to print the output on the right. Each print statement only prints out
one character at a time. So you can only use these print statements:
System.out.print("n");
System.out.println();
Here’s the output (Each row has nine n's):

nnnnnnnnn
nnnnnnnnn
nnnnnnnnn
nnnnnnnnn

54.) Use nested loops to print the output on the 55.) Use nested loops to print the

3
right. Each print statement only prints out one output on the right. Each print
number at a time. statement only prints out one
number at a time.
333
4444 7777777
55555 666666
666666 55555
7777777 4444
88888888

56.) Write a method named slope that accepts four integers as parameters: x1, y1,
x2, and y2. This method will return the slope based on the parameters. For example,
the call

slope(9, 3, 11, -6)

should return -4.5.

57.) Write a method named threeFourths that accepts an integer as an argument,


returning 3/4 multiplied by that integer argument. For example,

threeFourths( 7 )

should return 5.25, because 7 * 3 / 4 == 5.25.

58.) Write a method called averageOdds that takes five integers as parameters and
returns the average of all the odd numbers. For example,

averageOdds(9, 2, 7, 6, 4) would return 8.0,


because (9 + 7) / 2 == 8.0

averageOdds(15, 4, 3, 7, 1) would return 6.5,


because (15 + 3 + 7 + 1) / 4 == 6.5

59.) Write a method pear that takes an integer as a parameter, returning true if the number is
NOT a multiple of 6 and is divisible by 5, or if it is more than 5 digits, returning false otherwise.
You may assume that the parameter is positive. For example,
Method Call Returned Reason
value

pear( 35 ); true 35 is not a multiple of 6, and it is divisible by 5.

pear( 363636 ); true 363636 is more than 5 digits.

pear( 56 ); false 56 is not a multiple of 6, but it is not divisible by 5.


Also, 56 is not more than 4 digits long.

4
pear( 6000 ); false 6000 is divisible by 5, but it IS a multiple of 6.
Also, 6000 is not more than 4 digits long.

60.) Write a method myDawg that takes a dog's name, a number of spots (numSpots), and a
decimal weight (dogWeight) as parameters. It will return the following sentence:

"My dog name weighs dogWeight pounds and has numSpots spots."

For example,
Method call Returned value

myDawg("Skippy", 5, "My dog Skippy weighs 32.6 pounds and has 5 spots."
32.6);

myDawg("Dali", 60, 97.1); "My dog Dali weighs 97.1 pounds and has 60 spots."

61.) Write a method called skipPrint that accepts a String as its parameter and prints the
characters in order, skipping every other letter. For example, a call of skipPrint("zoidberg
lobster"); should print the following output:

zibr ose

If the empty string is passed, no output is produced. Your method should produce a
complete line of output.

62.) Write a method called reverse that accepts a String as its parameter and returns a
String with the characters in opposite order. For example, a call of reverse("hello
there!"); should return:

"!ereht olleh"

If the empty String is passed, an empty String will be returned.

63.) Write a method called printPowersOfN that accepts a base and an exponent as
arguments and prints each power of the base from base0 (1) up to that maximum power,
inclusive. For example, consider the following calls:
Method call Output in the terminal window

5
printPowersOfN(4, 3); 1 4 16 64

printPowersOfN(5, 6); 1 5 25 125 625 3125 15625

printPowersOfN(-2, 8); 1 -2 4 -8 16 -32 64 -128 256

64.) OPTIONAL, FOR EXTRA PRACTICE: Write a method called artificial that takes
two integers as parameters. If the first integer is divisible by 7 and second integer is smaller
than the first, then return the difference of the two integers (the first integer minus the second).
Otherwise, if the both integers are odd, then return the product of the two integers. Otherwise,
return the second integer. For example,
Method Call Returned Reason
value

artificial(28, 16 28 is divisible by 7, and 12 < 28, so return


12); the difference 28 - 12 == 16.

artificial(23, 207 23 is not divisible by 7, so we do not return


9); the difference. Therefore, check to see if
both numbers are odd. Since 23 and 9 are both
odd, then we return their product, 23 * 9 ==
207.

artificial(14, 29 14 is divisible by 7, but 29 < 14 is NOT true,


29); so we do not return the difference. Only one
integer is odd, not both of them, so we do not
return the product. Hence, we return the
second integer, which is 29.

65.) OPTIONAL, FOR EXTRA PRACTICE: Write a method countNegatives that takes
three decimals as parameters and returns how many of them are negative numbers. For
example
countNegatives( 8.2, -7.8, -3 ) returns 2.
countNegatives( -2.4, 30, 81 ) returns 1.
countNegatives( 2.4, 8, 3.14 ) returns 0.

66.) OPTIONAL, FOR EXTRA PRACTICE: Write a method wizowski that accepts an integer
as a parameter and returns true if the parameter is NOT divisible by 7, is odd, and greater
than 15, returning false otherwise. For example:
Method Call Returned value Reason

wizowski(17); true 17 is not divisible by 7, is odd, and is greater than 15.

wizowski(21); false 21 IS divisible by 7, so it doesn’t meet the criteria.

wizowski(22); false 22 is not divisible by 7, but it IS even, so it doesn’t


meet the criteria.

wizowski(9); false 9 is not divisible by 7, and it’s odd, but it’s NOT greater
than 15, so it doesn’t meet the criteria.
67.) OPTIONAL, FOR EXTRA PRACTICE: Write a method krattbros that accepts two
integers as parameters. The method returns true if the sum of both parameters is divisible
by 5 or if the second parameter is even. For example,
6
Method Call Returned Reason
value

krattbros( 9, 21 ); true 9 + 21 == 30, and 30 is divisible by 5.

krattbros( 11, 8 ); true The second parameter is 8, and 8 is even.

krattbros( 18, 5 ); false 18 + 5 == 23, which is not divisible by 5. Also, the second parameter
is 5, which is not even. So it meets none of the criteria.

Answers: Review for Test of Basic Java (Conditionals, Loops, Methods, Strings, DeMorgan's)
1.) 9 15.) integer: -501 to -322 32.) 15
33.) b
2.) -12.5 16.) 364
34.) r
17.) 200 35.) l
3.) 21 3 6
36.) tte
4.) 21 2 17 18.) 63 37.)
5.) 8 21 14 19.) 0, 11, 22, 33, ... , 77 mvanilla
38.)
6.) 24 15 5
20.) 4, 11, 18 upcakewars

//--------------------------------------------- 39.) alcupc


7.)
40.) 13
12 21.) 6 + Math.random() * 8
41.) 9
18
24 22.) 11 + Math.random() * 12 42.) -1
30 43.) 2
36 23.) 6 + (int) ( Math.random() * 24 )
44.) 7
8.) 72 24.) 762 + (int) ( Math.random() * 58 ) 45.) false

9.) 360 46.) 9


25.) -18 + (int) ( Math.random() * 12 )
47.) -4
10.) 17 20 23 26
26.) -32 + (int) (Math.random() * 83 ) 48.) -3
11.) _5_6_7_8_9 49.) 32
27.) Math.random() < 0.26
12.) 12 14 16 18 50.)
28.) (x <= y) || (y <= z ) rexlcupcxkewx
//---------------------------------------
29.) x != y && x > z rs
13.) decimal: 8 to 30.) (x / 2 != 13) && !b && (z * 3 != 96)
49.9999999 31.) (z >= x) || (z <= y) && (x < y)

14.) integer: 33 to 76

Answers: Review for Test of Basic Java (Conditionals, Loops, Methods) - (continued):

51.) // Method 1 51.) // Method 2

7
for (int i = -3; i <= 25 ; i += 7) { for (int i = 1; i <= 5; i++) {
System.out.print( i + " " ); System.out.print( (7 * i - 10) + " ");
} }

52.) // Method 1 52.) // Method 2

for (int i = 44; i >= -16; i -= 12) { for (int i = 1; i <= 6; i++) {
System.out.print( i + " " ); System.out.print( (-12 * i + 56) + " " );
} }

53.) 54.)
for (int i = 1; i <= 4; i++) for (int i = 3; i <= 8; i++)
{ {
for (int j = 1; j <= 9; j++) for (int j = 1; j <= i; j++)
{ {
System.out.print("n"); System.out.print(i);
} }
System.out.println(); System.out.println();
} }

55.) 56.)
for (int i = 7; i >= 4; i--) public double slope( int x1, int y1, int x2, int y2 )
{ {
for (int j = 1; j <= i; j++) return (double) (y2 - y1) / (x2 - x1);
{ }
System.out.print(i);
} 57.)
System.out.println(); public double threeFourths( int num )
} {
return num * 3 / 4.0 ;
}

58.) 59.) (Solution 1)


public double averageOdds ( int a, int b, int c,
int d, int x ) public boolean pear( int n )
{
{
double sum = 0.0;
int count = 0; if ( (n % 6 != 0 && n % 5 == 0) || n >= 100000 )
if ( a % 2 == 1 ) { {
sum += a; return true;
count ++; }
} else
if ( b % 2 == 1 ) { {
sum += b;
count ++; return false;
} }
if ( c % 2 == 1 ) { }
sum += c;
count ++;
} 59.) (Solution 2)
if ( d % 2 == 1 ) {
sum += d;
count ++; public boolean pear( int n )
} {
if ( x % 2 == 1 ) { return ( (n % 6 != 0 && n % 5 == 0)
sum += x; || n >= 100000 );
count ++;
} }
return sum / count;

8
}

60.)

public String myDawg( String name, int numSpots, double dogWeight )


{
return "My dog " + name + " weighs " + dogWeight + " pounds and has
"
+ numSpots + " spots.";
}

61.)
public void skipPrint( String str )
{
for ( int i = 0; i < str.length(); i += 2 )
{
System.out.print( str.charAt(i) );
}
}

// #62 (Method 1) // #62 (Method 2)


public String printReverse(String str) public String printReverse(String str)
{ {
String sol = ""; String sol = "";
int i = str.length() - 1; for ( int i = str.length() - 1; i >= 0; i-- )
while ( i >= 0 ) {
{ sol = sol + str.charAt(i);
sol = sol + str.charAt(i); }
i--; return sol;
} }
return sol;
}

// #63 (Method 1) // #63 (Method 2)


public void printPowersOfN(int base, int maxExp) public void printPowersOfN(int base, int maxExp)
{ {
int i = 0; for( int i = 0; i <= maxExp; i++ )
while( i <= maxExp ) {
{ System.out.print( (int) (Math.pow(base, i)) + " ");
System.out.print( (int) (Math.pow(base, i)) + " "); }
i++; }
}
}
.
64.) 65.)
public int artificial ( int a, int b )
{ public int countNegatives( double a, double b, double c )
if ( a % 7 == 0 && b < a ) {
{ int count = 0;
if ( a < 0 )
return a - b;
{
} count++;
else if ( a % 2 == 1 && b % 2 == 1 ) }
{ if ( b < 0 )
return a * b; {
9
} count++;
else }
{ if ( c < 0 )
return b; {
count++;
}
}
}
return count;
}

// #66 (Method 1)

public static boolean wizowski( int num )


{
return ( num % 7 != 0 ) && ( num % 2 == 1 ) && ( num > 15 );
}

// #66 (Method 2)

public static boolean wizowski( int num )


{
if ( ( num % 7 != 0 ) && ( num % 2 == 1 ) && ( num > 15 ) )
{
return true;
}
return false;
}

// #67 (Method 1)

public static boolean krattbros( int x, int y )


{
return ( (x + y) % 5 == 0 || y % 2 == 0 );
}

// #67 (Method 2)

public static boolean krattbros( int x, int y )


{
if ( (x + y) % 5 == 0 || y % 2 == 0 )
{
return true;
}
else
{
return false;

10
}
}

11

You might also like