Introduction to Computers and Programming
Midterm 2 Name: __________________________
1 (25 points) Write a method that returns the number of digits in a positive integer. Also, write main() which will call the method exactly 100 times using numbers generated from Math.random() in the range 0 – 10,000.
2. (25 points) Write a method raiseIntToPower that takes two integers, n and k, and returns nk. For example:
raiseIntToPower (2, 4);
would return 16
24 = 2 X 2 X 2 X 2
You only need to write the method declaration (including the body). Do not write main().
3. (20 points) What does the following code produce?
public class WhatDoIDo
public static void main (String  args)
public static void print(int number)
int index, uScores, numToShow;
numToShow = formula(number);
for(index = 1; index <= number; index++)
for(uScores = 1; uScores <= index; uScores ++)
public static int formula(int value)
return (value % 3) + 1;
4. (15 points) The following code contains at least 5 errors (syntax and or logic). Identify them (comments and formatting or lack thereof are not errors).
public class MyMax
public static void main(String args)
int max = maximum(5, 7);
System.out.println("Maximum is: " + maximum(5, 7, 3));
System.out.println("Maximum is: ", + max);
public static int maximum (int x, int y, int z)
int max = x;
if (y > max);
max = y;
if (z > max)
max = z;
public static int maximum (int x, y)
if (x > y)
5. (15 points)
a. Write a method header for a method that takes three integers as parameters and prints the largest of the three to the screen. (Only write the header)
b. How many times will the following loop execute the System.out.print statement?
a = 1;
while (a < 4)
a = a + a%3;
c. Given int a = 53, b = 4;
What will be the output of the following statement?
System.out.println ( (a % b) + (a / b) );
d. What does “void” mean when seen as the return type in a method? (one sentence answer only)
e. The following method header is part of the Math class. It returns the absolute value of its parameter. Write the line of code you would need to assign the absolute value of the variable number to a variable named absoluteValueOfNumber.
static double abs(double a)