= function(n){
f return(n*2)
}
# What does this display?
#f(3) + 100
# What does this display?
#x = f(3)
# What does this display?
#3
47 45. Practice Questions - TOPICS: cat, loops (reading code)
We went over many of these questions in our final review class (Fall 2023). In order to be able to answer these questions effectively it is imperative that you keep track the values of arguments and variables on a piece of paper as you mentally step through the code. Below are links to the videos from those review classes as well as PDFs of the notes that I took to keep track of the variables during those review classes.
-Prof. Rosenthal
Beren Review Class
Wilf Review Class
47.1 TOPICS: return vs cat
QUESTION 1 - Topics: return value of a function
What is displayed by the following code?
QUESTION 2 - Topics: cat
# What is displayed by the following code?
#cat(3)
# What is displayed by the following code?
#cat(3) * 2
# What is displayed by the following code?
#x = cat(3)
# What is displayed by the following code?
#x
QUESTION 3 - Topics: return vs cat
What is displayed by the following code?
= function(n){
f cat(n*2)
return(0)
}
# What does this display?
#f(3) + 100
# What does this display?
#x = f(3)
# What does this display?
#x
QUESTION 4 - Topics: return vs cat
What is displayed by the following code?
= function(n){
f cat(n*2)
}
# What does this display?
#f(3) + 100
# What does this display?
#x = f(3)
# What does this display?
#x
47.2 TOPICS: loops (for and while)
QUESTION 5 - Topics: for loops
What is displayed by the following code?
= function(n){
f
for (x in 1:n){
cat(x * x, " ")
}
cat("\n")
return(c(x, n))
}
# What does this display?
#result = f(4)
# What does this display?
#result
QUESTION 6 - Topics: while loops, cat vs return
What is displayed by the following code?
= function(n){
f
= 1
x while (n >= 1){
cat(x, " ")
= x * 2
x = n - 1
n
}
cat("\n")
return(c(x, n))
}
# What does this display?
#x = f(4)
# What does this display?
#x
QUESTION 7 - Topics: for loops
What is displayed by the following code?
= function(n){
f
for (x in 1:n){
cat("*** starting outer for loop again***\n")
for (y in x:n){
cat("x is ", x, " ")
cat("y is ", y, "\n")
}
}
return( c(x,y) )
}
# What does this display?
#result = f(4)
# What does this display?
#result
QUESTION 8 - Topics: for loops, nested loops, if
What is displayed by the following code?
= function(size){
drawSomething
if (!is.numeric(size) || trunc(size) != size || size < 0){
stop("size must be a non-negative whole number")
}
for (row in 1:size){
for (col in 1:size){
if (col == row || col == size - row + 1){
cat("@")
else {
} cat(".")
}
}
cat("\n")
}
cat("\nHope you like the picture! :)\n")
}
# What do these display?
#drawSomething(3)
#drawSomething(5)
#drawSomething(1)
#drawSomething(0) # be careful! This is tricky. Probably not what you'd expect.
QUESTION 9 - Topics: for loops, nested loops, if
The only thing that changed are the lines that are highlighted in the code with “# THIS CHANGED”.
What will be displayed now?
= function(size){
drawSomething
if (!is.numeric(size) || trunc(size) != size || size < 0){
stop("size must be a non-negative whole number")
}
for (row in 1:size){
for (col in 1:size){
if (col == row || col == size - row + 1){ # THIS CHANGED
# THIS CHANGED
if ( row %% 2 == 0) # THIS CHANGED
cat("0") # THIS CHANGED
else # THIS CHANGED
cat("X") # THIS CHANGED
# THIS CHANGED
else {
} cat(".")
}
}
cat("\n")
}
cat("\nHope you like the picture! :)\n")
}
# What does this display?
#drawSomething(5)
QUESTION 10 - Topics: for loops, nested loops, if
What is displayed by the following code?
= function(size){
drawSomething
if (!is.numeric(size) || trunc(size) != size || size < 0){
stop("size must be a non-negative whole number")
}
for(row in 1:size){
if( row == 1 || row == size ){
for (x in 1:size){
cat("-")
}cat("\n")
else {
} for (col in 1:size) {
if( row == 1 || row == size ){
for (x in 1:size){
cat("-")
}cat("\n")
else {
} if (col == 1){
cat("|")
else if (col == size){
} cat("|\n")
else if (row == col) {
} cat("x")
else {
} cat(".")
}
}
}
}
}
}
# What does this display?
#drawSomething(5)
# What does this display?
#drawSomething(10)