Skip to main content

✍️ Practice Test 2

To study for Test 2, try out the problems on paper prior to checking the solution. Review your labs too!

Question 1​

The code below contains syntax and logic errors. Find every error and correct them.

for(int i=42; i>=42; i++);
{
if (i == 50)
{
Console.WriteLine("wow so cool");
break;
}
if (i == "beans")
{
Console.WriteLine("???");
return;
}
}

Question 2​

The code below contains syntax and logic errors. Find every error and correct them.

static void WeirdFunction(int x; int y);
{
return "weird"
return "also weird"
}


WeirdFunction(2.0, 7, 40);

Question 3​

What is the output of the following program? After each line runs, write down the current value of every variable.

static void Main(int x)
{
int p, q, r, y=2; // p=_________ q=_________ r=_________ y=_________ x=_________
q = x--; // p=_________ q=_________ r=_________ y=_________ x=_________
r = 3; // p=_________ q=_________ r=_________ y=_________ x=_________
p = r; // p=_________ q=_________ r=_________ y=_________ x=_________
p += x; // p=_________ q=_________ r=_________ y=_________ x=_________
Console.WriteLine("p=" + p + " q=" + q + " r=" + r + " x=" + x);
x = ++r; // p=_________ q=_________ r=_________ y=_________ x=_________
p = Compute(y); // p=_________ q=_________ r=_________ y=_________ x=_________
r += 5; // p=_________ q=_________ r=_________ y=_________ x=_________
Console.WriteLine("p=" + p + " q=" + q + " r=" + r + " x=" + x + " y=" + y);
}

static int Compute(int x)
{
int result; // result=_________ temp=_________ x=_________
int temp = 6; // result=_________ temp=_________ x=_________
x += 2; // result=_________ temp=_________ x=_________
result = x + temp; // result=_________ temp=_________ x=_________
Console.WriteLine("temp=" + temp + " y=" + x);
return result; // result=_________ temp=_________ x=_________
}

Main(10);

Write the exact text that each Console.WriteLine statement displays on the screen.

Question 4​

Consider the following code where a user enters 10:

// initialize
int score = 0;

// user inputs 10
Console.Write("Enter a cool number: ");
int userInput = int.Parse(Console.ReadLine());

for (int num = 3; num < 6; num++)
{
score += userInput / num;
}

Trace the loop below by filling in one row per iteration. Leave any unneeded rows blank.

iterationscoreuserInputnum
start010-
1
2
3
4
5
6

Question 5​

The program below checks whether a file will fit on a device. All sizes are in gigabytes (GB):

// Inputs
Console.Write("Total storage (GB): ");
double totalSpace = double.Parse(Console.ReadLine());
Console.Write("Storage already used (GB): ");
double usedSpace = double.Parse(Console.ReadLine());
Console.Write("Size of the file you want to download (GB): ");
double wantedSpace = double.Parse(Console.ReadLine());

// Calculations
double remainingSpace = totalSpace - usedSpace;
bool canStore = CanStoreData(wantedSpace, remainingSpace);

// Outputs
if(canStore)
{
Console.WriteLine("Enough space! Download Complete <3");
}
else
{
Console.WriteLine("Not enough space. Download FAILED >:[");
}

Write the missing function in C# below without changing anything in the above program:

Question 6​

Welcome to the American Student Debt Calculator 5000!

Most American college graduates leave school with student loans, and many don’t know how long repayment will take or how much of their money will go to interest. Your job is to write a program that tells them.

Background

  • Federal undergraduate student loans have a fixed annual interest rate of 6.52%.
  • A dependent undergraduate can borrow up to $27,000 in federal loans over four years.

Each month, interest is added to the balance and then the payment is subtracted:

Interest=CurrentDebt∗(YearlyInterestRate/12)/100Interest = Current Debt * (Yearly Interest Rate/12)/100

UpdatedCurrentDebt=CurrentDebt+Interest−MonthlyPaymentUpdated Current Debt = Current Debt + Interest - Monthly Payment

Design a program that

  • Asks the user how much they owe and how much they plan to pay each month
  • Simulates repayment month by month until the debt is paid off
  • Prints how long repayment takes (in years and months) and the total interest paid

Turn your prior response into a short C# program