TCS Programming Questions on Functions and Scope – 2

1. What is the difference between a function and a method?
a. Function is a named code unlike method which is a part of an object
b. Function contained in an object is called a method
c. Function cannot change variables outside its scope unlike method
d. There is no difference between the two
Answer : a

2. Consider the following code:
function modify(a,b)
{
Integer c,d=2
c= a*d+ b
return c
}
function calculate()
{
integer a = 5, b = 20, c
integer d= 10
c = modify(a, b);
c = c+ d
print c
}
a. 80
b. 40
c. 32
d. 72
Answer: b
Explaination : c=? d=2 c=a*d+b c=a*2+b then a=5 b=20 , c=? c=c+d c=a*2+b+d c=5*2+20+10=40

3. What is the term given to the variable whose scope is beyond all the scopes i.e., it can be accessed by all the scopes?
a. Universal Variable
b. Global Variable
c. External Variable
d. Auto Variable
Answer : b

4.Anu wants to make a function that is not bound to any identifier.which of the following functions should she incorporate in her program?
a. Anonymous Function
b. Friend Function
c. Null Function
d. Global Function
Answer: a

5. Which of the following accessibility modes can be the specifier of a top level class’?Top-level classes can only have public, abstract, and final modifiers, and it is also possible to not define any class modifiers at all. This is called default/package accessibility. Besides that, private, protected, and static modifiers cannot be used when declaring top-level classes. Private, Protected, Public, No Modifier
a. Only Private
b. Protected and Private
c. Public and No Modifier
d. Only No Modifier
Answer: c

6. Top-level classes can only have public, abstract, and final modifiers, and it is also possible to not define any class modifiers at all. This is called default/package accessibility. Besides that, private, protected, and static modifiers cannot be used when declaring top-level classes.
Question 6 WRONG
Which of the following accessibility modes can be the specifier of a top level class’?Top-level classes can only have public, abstract, and final modifiers, and it is also possible to not define any class modifiers at all. This is called default/package accessibility. Besides that, private, protected, and static modifiers cannot be used when declaring top-level classes. Private, Protected, Public, No Modifier
a. Only Private
b. Protected and Private
c. Public and No Modifier
d. Only No Modifier
Answer:c

7.Top-level classes can only have public, abstract, and final modifiers, and it is also possible to not define any class modifiers at all. This is called default/package accessibility. Besides that, private, protected, and static modifiers cannot be used when declaring top-level classes.
Question 7 WRONG
Choose the correct answer. A pseudo-code which is similar to that of C++ and self-explanatory An accessible member function or data member for an object are accessed by the statement objectname.functionname or objectname. data member name respectively. class brush { Private: integer size, colorcode function getdata( ) {–}//Statement 1 public: integer name // Statement 2 function putdata(){…} } function main { brush b1, b2 print bl.name //Statement 3 b2.getdata() //Statement 4 } Deleting which line will correct the error in the code?
a.Statement 1
b.Statement 2
c.Statement 3
d.Statement 4
Answer:d
Explaination : Since, get data is private

8.Function MyDisplay(string MyStr) //statement 1 { print “Hello !” print MyStr return 1 // statement 2 } function main() //statement 3 { string str= “Mickey” MyDisplay(str) // statement 4 } Which statement will generate an error.
a.Statement 1
b.Statement 2
c.Statement 3
d.Statement 4
Answer:b

9.Choose the correct answer Tanuj writes the code for a function that takes as input n and calculates the sum of first n natural numbers. Function sum( n ) { if(??) return 1 else return (n + sum(n-1)) end } Fill in ?? in the code.
a. n equals 1
b. n equals 2
c. n >= 1
d. n > 1
Answer: a

10.Choose the correct answer Shrishti writes the code for a function that computes the factorial of the inputted number n. function factorial(n) { if(n equals 1) return 1 else — MISSING STATEMENT — end } Fill in the missing statement.
a.return factorial(n-1)
b.return n*factorial(n)
c.return n*(n-1)
d.return n*factorial(n-1)
Answer: d