Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

1. Which of the following code skips the first 20 iterations ?
a)

```for(i in 1:100) {
if(i <= 20) {
next
}
}```

b)

```for(i in 1:100) {
if(i <= 19) {
next
}
}```

c)

```for(i in 1:100) {
if(i <= 21) {
next
}
}```

d) All of the mentioned

Answer: a [Reason:] next is used to skip an iteration of a loop.

2. Point out the correct statement :
a) The only way to exit a repeat loop is to call break
b) Infinite loops should generally be avoided
c) Control structures like if, while, and for allow you to control the flow of an R program
d) All of the mentioned

Answer: d [Reason:] Control structures are primarily useful for writing programs; for commandline interactive work.

3. _________ initiates an infinite loop right from the start.
a) never
b) repeat
c) break
d) set

Answer: b [Reason:] These are not commonly used in statistical or data analysis applications but they do have their uses.

4. Which of the following code snippet stops loop after 20 iterations ?
a)

```for(i in 1:100) {
print(i)
if(i > 20) {
break
}
}```

b)

```for(i in 1:100) {
print(i)
if(i > 19) {
break
}
}```

c)

```for(i in 1:100) {
print(i)
if(i < 20) {
break
}
}```

d) All of the mentioned

Answer: a [Reason:] break is used to exit a loop immediately, regardless of what iteration the loop may be on.

5. Point out the wrong statement :
a) Statements cannot be grouped together using braces ‘{’ and ‘}’
b) Computation in R consists of sequentially evaluating statements
c) Single statements are evaluated when a new line is typed at the end of the syntactically complete statement
d) None of the mentioned

Answer: a [Reason:] Blocks are not evaluated until a new line is entered after the closing brace.

6. _______ is used to skip an iteration of a loop.
a) next
b) skip
c) group
d) all of the mentioned

Answer: a [Reason:] No statement below next in the current loop is evaluated.

7. Which of the following code can be used to avoid numeric problems such as taking the logarithm of a negative number ?
a)

` if( any(x < 0) ) y <- log(1+x) else y <- log(x)`

b)

` if( any(x <= 0) ) y <- log(1+x) else y <- log(x)`

c)

` if( any(x >= 0) ) y <- log(1+x) else y <- log(x)`

d) All of the mentioned

Answer: b [Reason:] The else clause is optional. The statement if(any(x <= 0)) x <- x[x <= 0] is valid.

8. R has ________ statements that provide explicit looping.
a) two
b) three
c) four
d) all of the mentioned

Answer: b [Reason:] They are for, while and repeat.

9. The syntax of the repeat loop is :
a) rep statement
b) repeat statement
c) repeat else
d) none of the mentioned

Answer: b [Reason:] The repeat statement causes repeated evaluation of the body until a break is specifically requested.

10. Which will be the output of following code ?

```> x <- 3
> switch(x, 2+2, mean(1:10), rnorm(5))```

a) 2.2903605 2.3271663 -0.7060073 1.3622045 -0.2892720
b) 5.5
c) NULL
d) All of the mentioned