Wednesday, January 6, 2016

C Programming::Function



FUNCTIONS


CALL BY VALUE
CALL BY REFERENCE
 
 
 
 
 
 
 
1.Consider the following code:
function modify(y,z)
 {
 y = y + 1;
  z = z + 1;
  return y ­ z
 }
function calculate( )
{  
int a = 5, b = 10, c;
 c = modify(a, b);  
print a   print space
 print c
 }
Assume that a and b were passed by value.
What will be the output on executing function calculate( )?

Op 1: 11 ­5
Op 2: 10 ­5
Op 3: 6 ­5
Op 4: 5 ­5

Correct Op : 4

2. Consider the following code:
function modify(b,a)
 {  
return a ­ b
 }
 function calculate( )
 {  
integer a = 5, b = 12, c  
   c = modify(a, b);
 print c
 }
Assume that a and b were passed by reference.
 What will be the output of the program on executing function calculate( ) ?

Op 1: 7
Op 2: ­7
Op 3: Error
Op 4: 8

Correct Op : 1

3. Consider the following code:
function modify(y,z)
 {
 y = y + 1
  z = z + 1
  return y ­ z
}
function calculate( )
{  
integer a = 12, b = 20, c
    c = modify(a, b);
  print a
 print space
 print c
 }
Assume that a and b were passed by reference. What will be the output of the function calculate( ) ?
Op 1: 12 ­8
Op 2: 13 ­8
Op 3: 12 8
Op 4: 13 8 

Correct Op : 2

4. Afzal writes a piece of code, where a set of three lines occur around 10 times in different parts of the program.
 What programming concept can he use to shorten his program code length?

 Op 1: Use for loops
Op 2: Use functions
Op 3: Use arrays
Op 4: Use classes   

Correct Op : 2

5. Geetika writes a piece of code, where a set of eight lines occur around 10 times in different parts of the program (Code A). She passes on the code to Deva.
 Deva puts the set of eight lines in a function definition and calls them at the 10 points in the program (Code B).
 Which code will run faster using an interpreter?

 Op 1: Code A
Op 2: Code B
Op 3: Code A and Code B will run with the same speed
Op 4: None of these

Correct Op : 1

6. 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
}
Assume that a and b were passed by value. What will be the output of the function calculate( ) ?
Op 1: 80
Op 2: 40
Op 3: 32
Op 4: 72

Correct Op : 2

7. Consider the following code:
function modify(w,u)
{
  w = w + 2
 u = u ­ 3
 return (w ­ u)
}
function calculate( )
{
 integer a = 10, b = 20, c
  c = modify(a, b);
  print a
  print space
  print b
 }
Assume that a was passed by value and b was passed by reference. What will be the output of the program on executing function calculate( ) ?
 Op 1: 12 17
Op 2: 10 17
Op 3: 12 20
Op 4: 10 20

 Correct Op : 2

8. Consider the following function:
function run( )
{  
integer a = 0 // Statement 1  
while (a < 5)
  {  
  integer c = 0   // Statement 2  
  c = c + 1   // Statement 3   
 a = a + 1
 }   print c   // Statement 4
 }
At which statement in this program will the compiler detect an error?
Op 1: Statement 1
Op 2: Statement 2
Op 3: Statement 3
Op 4: Statement 4

Correct Op : 4

9. Which one of the following is the lowest level format to which the computer converts a higher language program before execution?
 Op 1: English code
Op 2: Machine Code
Op 3: Assembly Language
 Op 4: System Language  

Correct Op : 2

10. If you want to write a function that swaps the values of two variables, you must pass them by:
Op 1: Value only
Op 2: Reference only
Op 3: Either A or B
Op 4: Neither A nor B

 Correct Op : 2

11. Consider the following code:
 if (condition 1)
 {
  if (condition 2)
 { 
 // Statement A  
}
  else     
if (condition 3)     
{
// Statement B
}  
  else    
 {
// Statement C
}
 else 
  if (condition 4) 
  { // Statement D }
 else   
{ // Statement E}
}
Which of the following conditions will allow execution of statement C?
Op 1: condition1 AND condition3
Op 2: condition1 AND condition4 AND !condition2
Op 3: NOT(condition2) AND NOT(condition3)
Op 4: condition1 AND NOT(condition2) AND NOT(condition3)

  Correct Op : 4

12. Consider the following code:
if (condition 1)
{
if (condition 2)
   {
// Statement A
 }
 else  if (condition 3) 
    {
// Statement B
}  
  else    
 {
// Statement C
}
else if (condition 4) 
  {
// Statement D
}  
else 
 {
// Statement E
}
}
Which of the following conditions will allow execution of statement E?
Op 1: condition1 AND condition3
Op 2: NOT(condition1) AND condition2 AND NOT(condition4)
Op 3: NOT(condition2) AND NOT(condition3)
Op 4: condition1 AND condition4 AND NOT(condition2) AND NOT(condition3)

Correct Op : 2

13. Consider the following code:
if (condition 1)
{
if (condition 2)
  {
// Statement A
 }  
else if (condition 3)     
{
// Statement B
}    
else     
{
// Statement C
}
 else if (condition 4)   
{
// Statement D
}
 else   
{
// Statement E
}
}

Which of the following condition will allow execution of statement A?
Op 1: NOT(condition2) AND NOT(condition3)
Op 2: condition1 AND condition4 AND NOT(condition2) AND NOT(condition3)
Op 3: condition1 AND condition2 AND condition4
Op 4: NOT(condition1) AND condition2 AND NOT(condition4)  

Correct Op : 3

14. What does the following function do?
 function operation (int a, int b) 
{
if (a < b)     
{
return operation(b, a)
 }
else 
 {
return a
}
 }

Op 1: Returns the max of (a,b)
Op 2: Returns the min of (a,b)
Op 3: Loops forever
Op 4: Always returns the second parameter

 Correct Op : 1

15. What does the following function do?
function operation (int a, int b) 
{
 if (a > b) 
 { return operation(b, a)
}  
else 
 {
 return a;
 }
}

Op 1: Always returns the first parameter
Op 2: Returns the min of (a,b)
Op 3: Returns the max of (a,b)
Op 4: Loops forever

  Correct Op : 2

16.
           function g(int n)
 {
 if (n > 0) return 1;
 else return ­1;

function f(int a, int b)
{
if (a > b) return g(b­a);
if (a < b) return g(a­b);
 return 0;
 }
If f(a,b) is called, what is returned?
Op 1: Always ­1
Op 2: 1 if a > b, ­1 if a < b, 0 otherwise
Op 3: ­1 if a > b, 1 if a < b, 0 otherwise
Op 4: 0 if a equals b, ­1 otherwise

  Correct Op :
 4

17.

 function g(int n)
 {
 if (n > 0)
return 1;
 else return ­1;
 } 
function f(int a, int b)
{
if (a > b)
return g(a­b);
 if (a < b)
return g(b­a);
 return 0;
 }

If f(a,b) is called, what is returned?
 Op 1: 1 if a > b, ­1 if a < b, 0 otherwise
 Op 2: Always +1
 Op 3: 0 if a equals b, +1 otherwise
 Op 4: ­1 if a > b, 1 if a < b, 0 otherwise

  Correct Op : 3

18.
function g(int n)
{
 if (n > 0)
 return 1;
else return ­1;
 } 
function f(int a, int b)
{
if (a > b)
 return g(a­b);
 if (a < b) return g(­b+a);
 return 0;
 }

 If f(a,b) is called, what is returned?
Op 1: Always +1
Op 2: 1 if a > b, ­1 if a < b, 0 otherwise
Op 3: ­1 if a > b, 1 if a < b, 0 otherwise
Op 4: 0 if a equals b, ­1 otherwise

 Correct Op : 2

19.

function g(int n)
{ if (n > 0)
return 1;
else return ­1;
 } 
function f(int a, int b)
 {
if (a > b)
return g(b­a);
if (a < b)
 return g(­a+b);
return 0;
}
If f(a,b) is called, what is returned?
Op 1: Always +1
Op 2: ­1 if a > b, 1 if a < b, 0 otherwise
Op 3: 1 if a > b, ­1 if a < b, 0 otherwise
Op 4: 0 if a equals b, ­1 otherwise

  Correct Op : 2

20. Consider the following code:
for i= m to n increment 2
 {
 print "Hello!"
 }
  Assuming m < n and exactly one of (m,n) is even, how many times will Hello be printed?
Op 1: (n ­ m + 1)/2
Op 2: 1 + (n ­ m)/2
Op 3: 1 + (n ­ m)/2 if m is even, (n ­ m + 1)/2 if m is odd
Op 4: (n ­ m + 1)/2 if m is even, 1 + (n ­ m)/2 if m is odd

  Correct Op : 1

 
 

No comments:

Post a Comment