CS111 Introduction to Computer Science Midterm 2 Fall 2023
Name: ______________________ NetID: ___________
● Write your name and NETID on EVERY page.
● DO NOT REMOVE THE STAPLE IN YOUR EXAM.
● DO NOT BEGIN UNTIL INSTRUCTED TO DO SO.
● WRITE NEATLY AND CLEARLY. If we cannot read your handwriting, you will not
receive credit.
● Please plan your space usage. No additional paper will be given.
● This exam is worth 150 points.
Section 1: Weeks 1-4 (35 points)
1) (8 points) For each of the following scenarios, identify the best data type. Choose from any
primitive data type, as well as String. Give a 1-line justification of why it is the best data type for
the variable.
a) (2 points) Price of a product
Data Type:
Explanation:
b) (2 points) Rutgers NetID (the ID comprised of letters and numbers)
Data Type:
Explanation:
c) (2 points) Age
Data Type:
Explanation:
d) (2 points) Letter Grade
Data Type:
Explanation:
1
CS111 Introduction to Computer Science Midterm 2 Fall 2023
Name: ______________________ NetID: ___________
2) (15 points) Consider the following code snippet.
a) (10 points - 2 points each row) What are the values of m and n at the end of each
iteration? Fill in the table below.
Iteration m n
0 0 54321
b) (5 points) What is the above code snippet doing in terms of n? Explain in 1-2 sentences.
3) (12 points - 3 points each) Consider the following code snippet. What is the output of this
code snippet with the following inputs?
a) x=0, y=0 ________________
b) x=2, y=3 ________________
c) x=3, y=2 ________________
d) x=2, y=0 ________________
2
CS111 Introduction to Computer Science Midterm 2 Fall 2023
Name: ______________________ NetID: ___________
Section 2: Arrays (35 points)
1) (15 points) Write a snippet of code that creates an integer array of size 10 and populates each
entry using the formula 2+3*index. For example, index 5 will contain the value 2 + 3 * 5=17.
Print out each array entry on a new line. You DO NOT need to include the headers (public
class/public static void main).
2) (10 points) Consider the following code snippet and line numbers.
a) (2 points) What is line 1 doing?
b) (2 points) What does arr[r].length represent in line 5? What is the numerical value?
c) (2 points) What is happening in line 6?
3
CS111 Introduction to Computer Science Midterm 2 Fall 2023
Name: ______________________ NetID: ___________
d) (4 points) In 1-2 sentences, describe what the code snippet is printing? What is the actual
output?
3) (10 points) Write a code snippet that finds and prints the minimum value in a 2-dimensional
array of integers. Assume the array name is arr. You DO NOT need to include the headers
(public class/public static void main).
Section 3: Input/Output
1. (5 points) Write a program that reads two inputs from the command line as integer values and
displays the smallest of the two inputs. Assume the inputs are integers.
4
CS111 Introduction to Computer Science Midterm 2 Fall 2023
Name: ______________________ NetID: ___________
2) (10 points) What is the output of the program below when executed with the command
java M2 < [Link]?
Output: _________
3) (5 points) Write the command to execute the program in question 2 and save the output in a
file named [Link].
Section 4: Functions (30 points)
1) (5 points) Consider the following function below.
a) (1 point) What is the name of the function? ____________________
5
CS111 Introduction to Computer Science Midterm 2 Fall 2023
Name: ______________________ NetID: ___________
b) (1 point) What are the inputs for this function? ____________________
c) (1 point) What is the return type? ____________________
d) (2 points) What would we write in the main method to call this function with the base as
2 and the exponent as 4?____________________
2) (15 points) averageOf3
a) (10 points) Write a function averageOf3 that takes in 3 separate integers as parameters,
and returns the average of the three numbers. Include the whole method signature
(modifiers, return type, method name, inputs) and the body of the method.
b) (5 points) Write 1-2 lines of code that will call your function with the inputs 2, 3, and 5,
and print out the result.
6
CS111 Introduction to Computer Science Midterm 2 Fall 2023
Name: ______________________ NetID: ___________
3) (10 points) Write a function calculateWage that takes in a number of hours worked and an
hourly rate as parameters. The function returns the total wage. For example, if the 5 hours are
worked and the hourly rate is 13.50, the function will return 67.50.
Section 5: Recursion
1) (15 points) Consider the following recursive function foo() as well as the line numbers.
a) (2 points) Which line(s) handles the base case?___________
b) (3 points) What is the outcome if foo(0,0) is called? ______________
c) (4 points) What are the values returned for foo(4,2) and foo(5,3) respectively?
_____________
d) (6 points) Briefly (1-2 lines) explain the purpose of the function foo().
7
CS111 Introduction to Computer Science Midterm 2 Fall 2023
Name: ______________________ NetID: ___________
2) (15 points) Answer the following questions given the recursive function mysteryFunc.
a) (5 points) What is printed when the following code snippet is executed?
b) (5 points) What is printed when the following code snippet is executed?
c) (5 points) Describe, in a sentence, what mysteryFunc computes.