CA Technologies R Programming Interview Questions

CA Technologies R Programming Recently Asked Interview Questions Answers

What is Recursion in R?

calculate_sum() <- function(n) {
if(n <= 1) { return(n) } else { return(n + calculate_sum(n-1)) } } Output: > calculate_sum(4)
[1] 10
Here, calculate_sum(n-1) is been used to compute the addition up to that number.Let suppose the user passes 4 to

What is Recursive Function in R?

Recursive functions call themselves. They break down the problem into the smallest possible components. The function() calls itself within the original function() on each of the smaller components. After this, the results will put together to solve the original problem.

For Example:

 Factorial <- function(N)
{
if (N == 0)
return(1)
else
return( N * Factorial (N-1))
}
CA Technologies R Programming Recently Asked Interview Questions Answers
CA Technologies R Programming Recently Asked Interview Questions Answers

What are applications of Recursion?

a) Dynamic Programming

It is the process of avoiding recomputation. It is an essential tool for statistical programming. There are two types of dynamic programming:

i) Bottom-up dynamic programming

In this, we check function starting with smallest possible argument value.
All computed values will be stored in an array.
ii) Top-down dynamic programming

Save each computed value as the final act of a recursive function.
Check if pre-computed values exist as the first action.
b) Divide-And-Conquer Algorithms

It is a Common class of recursive function.
Common features of this algorithm are process inputs, divide the input into
Smaller portions, Recursive call(s) process at least one part.
Recursion may sometimes occur before the input is been processed.

What are Features of Recursion?

The use of recursion, often, makes code shorter and looks clean.
It is a Simple solution for a few cases.
It expresses in a function that calls itself.

What is OOP in R?

Object-oriented programming is a popular programming language. It allows us to construct modular pieces of code which are used as building blocks for large systems. R is a functional language. It also supports exists for programming in an object-oriented style. OOP is a superb tool to manage complexity in larger programs. It is particularly suited to GUI development. R has two different OOP systems, known as S3 and S4.

What is S3 in R?

The S3 class is used to overload any function. It means calling a different name of the function. It depends upon the type of input parameter or the number of a parameter).

What is S4?

The S4 class is a characteristic OOP system, but it is tricky to debug.

What is reference class?

Reference classes are the modern alternative for S4 classes.

How to create the S3 class?

We can show how to define a function that will create and return an object of a given class. A list is been created with the relevant members, the list’s class is set, and a copy of the list is been returned.

What is S4 Generic?

The function setGeneric is the call to make a new generic function.

> setGeneric("BMI", function(object) standardGeneric("BMI"))
> setMethod("BMI", "personS4", function(object) {
+ object@weight/object@height^2
+ })
> BMI(jimS4)

What is the apply() family in R?

Apply functions are a family of functions in base R. which allow us to perform an action on many chunks of data. An apply function is a loop, but run faster than loops and often must less code. There are many different apply functions. The called function could be:

There is some aggregating function. They include meaning, or the sum(includes return a number or scalar);
Other transforming or subsetting functions.
There are some vectorized functions. They return more complex structures like lists, vectors, matrices, and arrays.
We can perform operations with very few lines of code in apply().

What is sapply() in R?

A Dimension Preserving Variant of “sapply” and “lapply”
sapply is a user-friendly version. It is a wrapper of lapply. By default sapply returning a vector, matrix or an array.

Keywords
Misc, utilities

Usage

Sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
Lapply(X, FUN, ...)
Arguments

X – It is a vector or list to call sapply.
FUN – a function.
… – optional arguments to FUN.
simplify – It is a logical value which defines whether a result is been simplified to a vector or
matrix if possible?
USE.NAMES – logical; if TRUE and if X is a character, use X as names for the result unless it had names already.

How to construct a new S3 class?

For Example:

> jim <- list(height = 2.54 * 12 * 6/100, weight = 180/2.2,+ name = "James") > class(jim) <- "person" > class(jim)

We have now made an object of class person
We now define a print method.

> print(jim)
> print.person <- function(x, ...) { + cat("name:", x$name, "\n") + cat("height:", x$height, "meters", "\n") + cat("weight:", x$weight, "kilograms", "\n") + } > print(jim)
Note the method/class has the ”dot” naming convention of method.class.

What is inheritance in S3 class?

In S3, inheritance is achieved by the class attribute being a vector.
For Example:

> fit <- glm(rpois(100, lambda = 1) ~ + 1, family = "poisson" > class(fit)
> methods("residuals")
> methods("model.matrix")
If no method for the first is found, the second class is checked.

What are useful S3 method functions?

methods(“print”) and methods(class = “lm”)
getS3method(“print”,”person”) : Gets the appropriate method associated with a class, useful to see how a method is implemented.
Sometimes, methods are non-visible, because they are hidden in a namespace. Use getS3method or getAnywhere to get around this-
> residuals.HoltWinters
> getS3method("residuals.HoltWinters")
> getAnywhere("residuals.HoltWinters")

How to construct new S4 class?

velocity = c(0.0,0.0)
),
# Make a function that can test to see if the data is consistent.
# This is not called if you have an initialize function defined!
validity=function(object)
{
if(sum(object@velocity^2)>100.0) {
return("The velocity level is out of bounds.")
}
return(TRUE)
}
)
Now that the code to define the class is given we can create an object whose class is Agent.

> a <- Agent() > a
An object of class "Agent"
Slot "location":
[1] 0 0
Slot "velocity":
[1] 0 0
Slot "active":
[1] TRUE

What is the regular expression in R string manipulation?

A set of strings will define as regular expressions. We use two types of regular expressions in R, extended regular expressions (the default) and Perl-like regular expressions used by perl = TRUE.

What is regular expression syntax?

It specifies characters to seek out, with information about repeats and location within the string. This will practice with the help of metacharacters that have a specific meaning: $ * + . ? [ ] ^ { } | ( ) \.


Post a Comment

Previous Post Next Post