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?

f = function(n){
  return(n*2)
}

# What does this display?
#f(3) + 100

# What does this display?
#x = f(3)

# What does this display?
#3
# What does this display?
f(3) + 100
[1] 106
# What does this display?
x = f(3)
# What does this display?
x
[1] 6

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

The job of the cat function is simply to display information to the screen. You cannot capture the output of cat and use it. Similarly you cannot multiply the value that is displayed by cat or use the output of cat in any similar “larger context”.

This is very different from other functions, eg. seq. With most other functions you can capture the output in a variable or use the function in a larger context. The following examples show how the output of seq can be used in a larger context. You cannot do any of that with cat. The reason that you can use the output of most other functions in a larger context is that these other functions “return” values.

#######################################################################.
# Demonstrating the return value of seq being used in a larger context
#######################################################################.

# We can use the function by itself and the return value will be displayed to
# the screen.
seq(1,3)         
[1] 1 2 3
############################################################################.
# The following commands use the value that is returned by seq in a
# larger context
############################################################################.
sum(seq(1,3))    # sum the values
[1] 6
seq(1,3) * 100   # multiply the values
[1] 100 200 300
x = seq(1,3)     # save the values in a variable
x                # now the variable has the value
[1] 1 2 3
###########################################################.
# cat is different - it's return value is NULL
###########################################################.

# As we said, cat displays values to the screen but does not "return" those
# values. That means that you cannot use those values in a "larger context".
#
# The actual return value of cat is the special value NULL. NULL basically
# means "there is NOTHING being returned" (NA is different than NULL in that
# NA stands for a value that technically exists, we just don't know the actual
# value). One last source of confusion is that you don't normally see 
# the value NULL when you run cat since the NULL is returned as an 
# "invisible" value (see ?invisible).
#
# The following shows what happens when you try to use cat in a larger
# context. 
# What is displayed by the following code?
#
# ANSWER: the info is displayed on the screen.
cat(3) 
3
# What is displayed by the following code?
#
# ANSWER: 
#   Step 1 - cat displays 3 on the screen.
#   Step 2 - cat returns NULL
#   Step 3 - NULL * 2 results in numeric(0). Basically, the idea is that you 
#            cannot use the output of cat in a meaningful way.

cat(3) * 2
3
numeric(0)
# What is displayed by the following code?
#
# ANSWER: 
#   Step 1 - cat displays 3 on the screen.
#   Step 2 - the NULL that is "returned" from cat is captured
#            in the variable x
x = cat(3)
3
# What is displayed by the following code?
#
# ANSWER: 
#   The value NULL that was assigned to x is displayed.
x
NULL

QUESTION 3 - Topics: return vs cat

What is displayed by the following code?

f = function(n){
  cat(n*2)
  return(0)
}
# What does this display?
#f(3) + 100
# What does this display?
#x = f(3)
# What does this display?
#x

There is a big difference between cat and return. Values that are returned can be used in a larger context when calling a function.

Values that are displayed with cat appear on the screen, but they are NOT returned. This means that you cannot capture the values that are displayed with cat in a variable. Similarly the values that are displayed with cat CANNOT be used in a larger context.

The return value of f is 0. It is this 0 that is used in a larger context. The call to cat in the function simply displays info to the screen but that info cannot be used in any larger context.

# What does this display?
#
# ANSWER: 
#   step 1 - n*2 is calculated to be 6
#   step 2 - cat displays 6 on the screen
#   step 3 - the function f "returns" 0
#   step 4 - 0 + 100 is calculated to be 100 and is displayed to the screen.

f(3) + 100
6
[1] 100
# What does this display?
#
# ANSWER: 
#   step 1 - n*2 is calculated to be 6
#   step 2 - cat displays 6 on the screen
#   step 3 - the function f "returns" 0
#   step 4 - x is assigned the return value of 0
x = f(3)
6
# What does this display?
#
# ANSWER:
#   The value 0 that was assigned to x is displayed

x
[1] 0

QUESTION 4 - Topics: return vs cat

What is displayed by the following code?

f = function(n){
  cat(n*2)
}

# What does this display?
#f(3) + 100

# What does this display?
#x = f(3)

# What does this display?
#x

There is a big difference between cat and return. Values that are returned can be used in a larger context when calling a function.

Values that are displayed with cat appear on the screen, but they are NOT returned. The actual “return value” of cat is NULL. Since cat is the last thing to be executed in the function, the return value of the function is also NULL.

# What does this display?
#
# ANSWER: 
#   step 1 - n*2 is calculated to be 6
#   step 2 - cat displays 6 on the screen
#   step 3 - cat "returns" NULL
#   step 4 - since cat is the last thing to be executed in the function,
#            the function, f, returns NULL.
#   step 5 - NULL + 100 results in numeric(0) and that is displayed

f(3) + 100
6
numeric(0)
# What does this display?
#
# ANSWER: 
#   step 1 - n*2 is calculated to be 6
#   step 2 - cat displays 6 on the screen
#   step 3 - cat "returns" NULL
#   step 4 - since cat is the last thing to be executed in the function,
#            the function, f, returns NULL.
#   step 5 - x is assigned the value NULL

x = f(3)
6
# What does this display?
#
# ANSWER: 
#   step 1 - The value NULL that was assigned to x is displayed to the screen
x
NULL

47.2 TOPICS: loops (for and while)

QUESTION 5 - Topics: for loops

What is displayed by the following code?

f = function(n){
  
  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
# What does this display?
result = f(4)
1  4  9  16  
# What does this display?
result
[1] 4 4

QUESTION 6 - Topics: while loops, cat vs return

What is displayed by the following code?

f = function(n){
  
  x = 1
  while  (n >= 1){
    cat(x, " ")
    x = x * 2
    n = n - 1
  }
  
  cat("\n")
  
  return(c(x, n))

}

# What does this display?
#x = f(4)

# What does this display?
#x
# What does this display?
x = f(4)
1  2  4  8  
# What does this display?
x
[1] 16  0

QUESTION 7 - Topics: for loops

What is displayed by the following code?

f = function(n){
  
  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
# What does this display?
result = f(4)
*** starting outer for loop again***
x is  1     y is  1 
x is  1     y is  2 
x is  1     y is  3 
x is  1     y is  4 
*** starting outer for loop again***
x is  2     y is  2 
x is  2     y is  3 
x is  2     y is  4 
*** starting outer for loop again***
x is  3     y is  3 
x is  3     y is  4 
*** starting outer for loop again***
x is  4     y is  4 
# What does this display?
result
[1] 4 4

QUESTION 8 - Topics: for loops, nested loops, if

What is displayed by the following code?

drawSomething = function(size){
  
  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.
# What do these display?
drawSomething(3)
@.@
.@.
@.@

Hope you like the picture! :)
drawSomething(5)
@...@
.@.@.
..@..
.@.@.
@...@

Hope you like the picture! :)
drawSomething(1)
@

Hope you like the picture! :)
drawSomething(0)  # be careful! This is tricky. Probably not what you'd expect.
@@
@@

Hope you like the picture! :)

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?

drawSomething = function(size){
  
  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)
# What does this display?
drawSomething(5)
X...X
.0.0.
..X..
.0.0.
X...X

Hope you like the picture! :)

QUESTION 10 - Topics: for loops, nested loops, if

What is displayed by the following code?

drawSomething = function(size){
  
  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)
# What does this display?
drawSomething(5)
-----
|x..|
|.x.|
|..x|
-----
# What does this display?
drawSomething(10)
----------
|x.......|
|.x......|
|..x.....|
|...x....|
|....x...|
|.....x..|
|......x.|
|.......x|
----------