Dee Ruttenberg
  • Home
  • About
  • Values
  • Publications
  • Blog
  • Labs

On this page

  • Practice Exam

Practice Exam 1

Practice Exam, and general R practice

Author

Dee Ruttenberg

Published

February 11, 2026

Practice Exam

  1. You run the following code
sum = 0 
list = c(2:5)
for(i in list){
  sum = sum + i
}
  1. What would be the output of print(list)?

  2. When this function finishes running, what is sum equal to?

  1. You run the following code
x = 10
y = 5
while(x>y){
  x = x-2
}
  1. When you finish running this code, what will x equal?

  2. What will y equal?

  3. Consider this very slightly different code (remember, != means ‘not equal’). What is the problem with it?

x = 10
y = 5
while(x!=y){
  x = x-2
}
  1. Consider the following function, which takes the sum of a given number of pennies, nickels, dimes, and quarters:
coin_sum = function(p, n, d, q){
  c = p + 5*n + 10*d + 25*q
  if(c >= 100){
    return(c/100)
  }
  else{
    return (c)
  }
}
  1. Calculate coin_sum(1,1,1,1)

  2. Calculate coin_sum(0,0,0,4)

  3. What does the variable n mean in the function?

  4. Explain the purpose of the conditional (if c>=100...) in human terms. Why might we have it?