Programming Logic Concepts For TCS NQT

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Programming Logic Concepts For TCS NQT

Q1. 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

Q2. 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

Q3. 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

Q4.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

Q5. 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

Q6. 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

Q7) Function MyDisplay(string MyStr) //statement 1

print “Hello !”

print MyStr

return 1 // statement 2

function main() //statement 3

string str= “Mickey”

MyDisplay(str) //statement4

Which statement will generate an error..

A)Statement 1

B)Statement 2

C)Statement 3

D)Statement 4

Answer: B

Q8) 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

Q9) 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

Q10) What is the output of this C code?

#include <stdio.h>

int main() {

int y = 10000;

int y = 34;

printf(“Hello World! %d\n”,y);

return 0;

A)Compile time Error

B)Hello World! 34

C)Hello World! 1000

D)Hello World! Followed by a junk value

Answer:A

Explaination: Compile time error will be raised, as the compiler will get confused which of the value is
to be printed

Q11) 1.Choose the correct answer Saumya writes a code which has a function which calls itself. Which
programming concept is Saumya using?

A) This is bad programming practice and should not be done.

B) Recursion

C) Decision Making

D) Overloading
Answer:B

Q12) Consider the following function

function calculate( n )

if(n equals 5)

return 5

else

return (n + calculate(n-5))

end

Shishir calls the function by the statement, calculate(20). What value will the function return?

A) 50

B) 200

C) 35

D) 20

Answer: A

Explaination : its recursive fn.

calc(20) returns (20+calc(20-5))

=20+calc(15)

=20+15+calc(10)

=20+15+10+calc(5)

=20+15+10+5

=50

Q13) 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 :B

Sol: =3+sum(2)

=3+2+sum(1)

Q14) Choose the correct answer

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

A) Use for loops

B) Use functions

C) Use arrays

D) Use classes

Answer: B

Explanation : for example if afzal wants to take a input several times, then he must declare a input
function and call it anytime when he wants to take the input
Q15) Talika wants to implement heterogeneous linked list for her project. Which of the following will
help her do the same.

A) Void pointer

B) Null pointer

C) Wild pointer

D) Heterogeneous list follows the same procedure as the homogeneous list. Hence no different pointer
is required.

Answer: A

Explanation : The heterogeneous linked list contains different data types in its nodes and we need a link,
pointer to connect them. It is not possible to use ordinary pointers for this. So we go for void pointer.
Void pointer is capable of storing pointer to any type as it is a generic pointer type.

Q16) In C, if you pass an array as an argument to a function, what actually gets passed?

A) Value of elements in array

B) First element of the array

C) Base address of the array

D) Address of the last element of array

Ans: C

Sol : The statement \'C\' is correct. When we pass an array as a funtion argument, the base address of
the array will be passed.

17) What does the following declaration mean?

int (*ptr)[10];

A. ptr is array of pointers to 10 integers

B. ptr is a pointer to an array of 10 integers


C. ptr is an array of 10 integers

D. ptr is an pointer to array

Answer: Option B

Sol : A. ptr is array of pointers to 10 integers. ptr is a pointer to an array of 10 integers. ptr is an array of
10 integers.

Q18) What will happen if in a C program you assign a value to an array element whose subscript exceeds
the size of array?

A) The element will be set to 0.

B) The compiler would report an error.

C) The program may crash if some important data gets overwritten.

D) The array size would appropriately grow.

Ans: C

Sol : If the index of the array size is exceeded, the program will crash. Hence “option c” is the correct
answer. But the modern compilers will take care of this kind of errors.

Q19) Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1?

A) rem=3.14%2.1

B) rem=modf(3.14,2.1)

C) rem=fmod(3.14,2.1)

D) reminder cannot be obtain in floating point decision

Answer: Option C

Sol: fmod(x,y) – Calculates x modulo y, the remainder of x/y.

This function is the same as the modulus operator. But fmod() performs floating point divisions.

Q20) What will be the output of the program?

#include<stdio.h>

int main()

int a[5] = {5, 1, 15, 20, 25};


int i, j, m;

i = ++a[1];

j = a[1]++;

m = a[i++];

printf("%d, %d, %d", i, j, m);

return 0;

A)2, 1, 15

B)1, 2, 5

C)3, 2, 15

D)2, 3, 20

Ans: C

Sol : Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5
and it is initiapzed to

a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .

Step 2: int i, j, m; The variable i,j,m are declared as an integer type.

Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2

Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.

Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)

Step 6: printf(“%d, %d, %d”, i, j, m); It prints the value of the variables i, j, m

Hence the output of the program is 3, 2, 15

Q21) What will be the output of the below program if the below code is run with three command line
parameters mentioned below:

Paper Ink Pen

#include

int main(int argc, char ** argv){


char **items;

int j = 3, i;

items = argv;

for(i = 1; (i%4); i++){

int **p = &items[j];

printf("%c", **p);

j--;

return 0;

A)PIP

B)Pen

C)Pap

D)Ink

Ans: A

Q22) What is the data type that occupies the least storage in “C” language?

A)int

B)char

C)float

D)double

Ans: 2

Sol: C is associated with different data types to each variable and occupies different amount of
memory. The char data type is used to store a single character and is the most basic data type in C. It
requires only one byte of memory for storage and can contain both positive and negative values.

Q23) Which of the following is true?

A) Array is a dynamic data structure whose size can be changed while stacks are static data structures
whose sizes are fixed.
B) Array elements can be accessed and modified(elements can be added or removed) only at the ends of
the array while any elements of the stack can be accessed or modified randomly through their indices.

C) An array can have elements of different data types.

D) Elements of a linked-list can be accessed only sequentially.

A) a

B) b

C) c

D) d

Ans. D

Sol: Elements of a linked list can be accessed only sequentially as on each node you will get the address
of the next node only, you cannot jump out any node

Q24) Improper formation of which of the following data-structures can cause un-intentional looping of a
program that uses it.

A) Linked List

B) Array

C) Queue

D) Stack

Ans: A

Sol : It has one common cause, for example, is that the programmer intends to iterate over sequence of
nodes in a data structure such as linked list or tree, executing the loop code once for each node

Q25) Which of the following statements is FALSE?

a) The time complexity of binary search is O(log n).

b) A linear search requires a sorted list.

c) A binary search can operate only on a sorted list.

d) The time complexity of linear search is O(n).

A) a

B) b
C) c

D) d

Ans: B

Sol : Linear search compares each of the element sequentially thats why is it does'nt need a sorted list,
its binary search that needs a sorted list

Q26) Eesha wrote a function fact( ) in “C” language to calculate factorial of a given number and saved
the file as fact.c. She forgot to code the main function to call this fact function. Will she be able to
compile this fact.c without the main() function?

A) Yes, she can compile provided the compiler option -no strict-checking is enabled.

B) No, she can not compile as the main function is required to compile any C program file.

C) Yes, she can compile as main( ) is not required at compile time.

D) Yes, she can compile and run as the system will supply default values to fact function.

Ans: B

Sol: Main() is the compulsory function in a code, to make it execute

Q27)The difference between variable declaration and variable definition is:

A) Declaration and definition are the same. There is no difference.

B) A declaration is used for variables and definitions is used for functions.

C) Declaration associates type to the variable whereas definition associates scope to the variable.

D) Declaration associates type to the variable whereas definition gives the value to the variable.

A) a

B) b

C) c

D)d

Ans: D
Sol: Declaration of a variable is for informing to the compiler the following information: name of the
variable, type of value it holds and the initial value if any it takes. i.e., declaration gives details about the
properties of a variable. Whereas, Definition of a variable says where the variable gets stored.

Q28) The inorder and preorder traversal of a binary tree are d b e a f c g and a b d e c f g, respectively.
The post-order traversal of the binary tree is:

A) d e b f g c a

B) d e f g c a

C) e d b f g c a

D) e d b f f c a

Ans: A

Sol: The inorder and preorder traversals of a binary tree T is DBHEAFGC and ABDEHCFG respectively. ...
So, the traversal of the null node is… null, the traversal of a single node is itself, and then you get into
some fun, based on finding that [root] entry in the traversallist.

Q29 ) How many times the below loop will be executed?

#include<stdio.h>

int main()

int x, y;

for(x=5;x>=1;x--)

for(y=1;y<=x;y++)

printf("%d\n",y);

A) 15

B) 11

C) 10
D) 13

Ans. A

Sol: The code will be executed 15 times, as the code has two loops which are inter-related

Q30) What is the output of this C code?

#include <stdio.h>

int main() {

printf(“Hello World! %d\n”, x);

return 0;

A) Hello World! X;

B) Hello World! Followed by a junk value

C) Compiler time error

D) Hello World!

Ans: C

Sol: %d has no relevance here

Q31)Which of the following is not valid variable name declaration?

A)int_v1;

B)int_1v;

C)int_V1

D)None

ANS: D

Q32)Variable names beginning with underscore is not encouraged. Why?

A) It is not standardized
B) To avoid conflicts since assemblers and loaders use such names

C) To avoid conflicts since library routines use such names

D) To avoid conflicts with environment variables of an operating system

Answer: Option C

Q33)Which is not a valid C variable name?

A)Int number;

B)Float rate;

C)Int variable_count;

D)Int $main;

Answer: Option D

Q34)Which of the following is true for variable names in C?

A)They can contain alphanumeric characters as well as special characters.

B)It is not an error to declare a variable to be one of the keywords(like goto, static)

C)Variable names cannot start with a digit.

D)Variable can be of any length.

Answer: Option C

SOL: According to the syntax for C variable name, it cannot start with a digit.

Q35)What will be the output?

#include <stdio.h>

int main()

{
int main = 5;

printf(“%d”, main);

return 0;

A)Compile time error.

B)Run time error.

C)Run without any error and print 5

D)Experience infinite looping

Ans: C

Q36)All keywords in C are in

A)Camelcase letters

B)Uppercase letter

C)Lowercase letters

D)None

Ans: C

Q37)The format identifier ‘%i’ is also used for _____ data type?

A)Char

B)Double

C)Float

D)Int

Ans: D

Q38)Which of the following is a User-defined data type?


A) typedef int Boolean;

B) typedef enum {Mon, Tue, Wed, Thu, Fri} Workdays;

C) struct {char name[10], int age};

D) All of the mentioned

Ans: D

Sol: typedef and struct are used to define user-defined data types.

Q39)What is short int in C programming?

A) short is the qualifier and int is the basic datatype

B) All are Qualifier

C) Basic data type of C

D) All of the mentioned.

Ans: A

Q40) What is the output of this C code?

#include

<stdio.h>

int main()

signed char chr;

chr = 128;

printf(“%d\n”, chr);

return 0;

A)128
B)-128

C)Depends on the compiler

D)None of the mentioned

Ans: B

Q41)Neelam wants to share her code with a colleague, who may modify it. Thus she wants to include
the date of the program creation, the author and other she wants to include the date of the program
creation, the author and other information with the program. What component should she use?

A)Header files

B)Iteration

C)Comments

D)Pre proccessor

Ans: C

Q42)What is the output of the following code statements? The compiler saves the first integer at the
memory location 4165 and the rest at consecutive memory spaces in order of declaration. Integer is one
byte long.

integer a

pointer c, d

a = 30c = &a

c = &a

d=c

a = a + 10

print *c

A)30

B)40

C)4165
D)4166

Ans: B

Q43)A data type is stored as a 6 bit signed integer. Which of the following cannot be represented by this
data type?

A)-12

B)32

C)18

D)6

Ans : B

Q44)A language has 28 different letters in total. Each word in the language iscomposed of maximum 7
letters. You want to create a data-type to store a word ofthis language. You decide to store the word as
an array of letters. How many bits will you assign to the data-type to be able to store all kinds of words
of the language.

A)35

B)28

C)7

D)196

Ans: A

Q45)A 10-bit unsigned integer has the following range:

A)0 to 1000

B)0 to 1024

C)0 to 1023

D)0 to 1025

Ans: C

Q46)Parul takes as input two numbers: a and b. a and b can take integer valuesbetween 0 and 255. She
stores a, b and c as 1-byte data type. She writes the following code statement to process a and b and put
the result in c.
c=a+2*bTo her surprise her program gives the right output with some input values of a and b, while
gives an erroneous answer for others. For which of the following inputs will it give a wrong answer?

A)a = 200 b = 10

B)a = 10 b = 200

C)a = 100 b = 50

D)a = 50 b = 100

Ans: B

Q47)Which is used to convert source code to target language

A)Linker

B)Loader

C)Executer

D)Compiler

Ans: D

Q48)Tricha wants to store a list of binary data.Which of following data types should she use?

A)Integer

B)Float

C)Character

D)Boolean

Ans: D

Q49)Which of the following options is an exception to being a part of composite data types?

A)Arrays

B)Structure

C)Union

D)Stacks

Ans: D
Q50)Which data type is most suitable for storing a number 65000 in a 32-bit system?

A)Short

B)Int

C)Long

D)Double

Ans: A

Q51)What is the size of an int data type?

A)4 Bytes

B)8 Bytes

C)Depends on the system/compiler

D)Cannot be determined

Ans:C

Q52)Which of the datatypes have size that is variable?

A)Int

B)Struct

C)Float

D)Double

Ans: B

Q53)What is the output of this C code?

#include <stdio.h>

int main()

{
float x = ‘a’;

printf(“%f”, x);

return 0;

A)97.000000

B)Run time error

C)a.0000000

D)a

Ans: A

Q54)Which is correct with respect to size of the datatypes?

A)Char > int > float

B)Int > char > float

C)Char > int > double

D)Double > char > int

Ans: C

Q55)Variable name resolving (number of significant characters for uniqueness of variable) depends on

A)Compiler and linker implementations

B) Assemblers and loaders implementations

C) C language

D) None of the mentioned

Answer: A

Explanation: It depends on the standard to which compiler and linkers are adhering.
Q56)Consider on following declaration:

(i) short i=10;

(ii)static i=10;

(iii)unsigned i=10;

(iv)const i=10;

A) Only (iv) is incorrect

B) Only (ii) and (iv) are incorrect

C) Only (ii),(iii) and (iv) are correct

D) All are correct declaration

Ans: D

Q57)Which of the following is not a valid variable name declaration?

A)Float PI = 3.14;

B)#define PI3.14;

C)Double PI= 3.14;

D)Int PI : 3.14;

Ans: B

Q58)The format identifier ‘%i’ is also used for _____ data type?

A)Char

B)Double

C)Int

D)Float

Ans: C
Q59)Literal means ?

A)A string

B)A string constant

C)A charatcter

D)An alphabet

Ans: B

Q60)What will happen if the below program is executed?

#include <stdio.h>

int main() {

int main = 3;

printf(“%d”, main);

return 0;

A)It will cause a compile time error

B)It will cause run time error.

C)It will run without any error and print 3

D)It will experience infinite looping

Ans: C

Q61.function main()

integer a=5,b=7

switch(a)

case 5 :print “I am 5”
break

case b:print “I am not 5”

break

default:print “I am different”

A)I am 5

B)I am not 5

C)I am different

D)Error

Answer:D

Explanation:

Break should have ;

for not showing error

Q62)Ashima wants to print a pattern which includes checking and changing a variables value iteratively
She decides to use a loop/condition Which of the following options should she use such that the body of
the loop/condition is executed atleast once whether the variable satisfies the entering condition or not?

A)For Loop

B)While Loop

C)Do While Loop

D)Switch Case

Answer:C

Sol: Do-while loop is the only loop which executes once, even if the starting condition is wrong
Q63)The construct “if (condition) then A else B” is for which of the following purposes? 1) 2) 3) 4)

A)Decision-Making

B) Iteration

C) Recursion

D) Object Oriented Programming

Answer:A

Sol: In this condition it is checked if the first condition is right then we'll go with A otherwise we'll go
with B

Q64)Ravi and Rupali are asked to write a program to sum the rows of 2X2 matrices stored in the array A.
Ravi writes the following code (Code A): for n = 0 to 1 sumRow1[n] = A[n][1] + A[n][2] end Rupali writes
the following code (Code B): sumRow1[0] = A[0][1] + A[0][2] sumRow1[1] = A[1][1] + A[1][2] Comment
upon these codes (Assume no loop- unrolling done by compiler):

A)Code A will execute faster than Code B.

B)Code B will execute faster than Code A

C)Code A is logically incorrect.

D)Code B is logically incorrect.

Answer:B

Sol: both codes are taking 2 steps of operation, therefore same complexity.

But if we consider the overhead of looping (as it takes time to increment counter) then code b will be
faster.

hence option 2. B is faster.

Q65.Integer a =40, b =35, c=20, d =10 Comment about the output of the following two statements •

Print a*b/c-d
Print a*b/(c-d)

Comment about the output of the following two statements

A)Differ by 80

B)Same

C)Differ by 50

D)Differ by 160

Answer:A

Q66)What is the output of the following pseudo code?

Int a =456,b,c,d=10;

b=a/d; c=a-b;

print c;

A)411.4

B)411

C)410.4

D)410

Answer: B

Sol:

)b=456/10=45(Quotient)

c=456-45=411

Q67) Function main()

Integer i=0.7

Static float m=0.7


If(m equals i)

Print(“We are equal”)

Else If(m>i)

Print(“I am greater”)

Else

Print(“I am lesser”)

A)We are equal

B)I am greater

C)I am lesser

D)This code will generate an error

Answer:D

Q68) Recursion is a method in which the solution of a problem depends on ____________

A) Larger instances of different problems

B) Larger instances of the same problem

C) Smaller instances of the same problem

D) Smaller instances of different problems

Answer: C

Explanation: In recursion, the solution of a problem depends on the solution of smaller instances of the
same problem.

Q69) Which of the following problems can be solved using recursion?

A) Factorial of a number

B) Nth fibonacci number

C) Length of a string
D) All of the mentioned

Answer: D

Explanation: All of the above mentioned problems can be solved using recursion.

Q70) Recursion is similar to which of the following?

A) Switch Case

B) Loop

C) If-else

D) None of the mentioned

Answer: b

Explanation: Recursion is similar to a loop.

Q71) In recursion, the condition for which the function will stop calling itself is ____________

A) Best case

B) Worst case

C) Base case

D) There is no such condition

Answer: C

Explanation: For recursion to end at some point, there always has to be a condition for which the
function will not call itself. This condition is known as base case.

Q72) Consider the following code snippet:

void my_recursive_function()

my_recursive_function();
}

int main()

my_recursive_function();

return 0;

What will happen when the above snippet is executed?

A) The code will be executed successfully and no output will be generated

B) The code will be executed successfully and random output will be generated

C) The code will show a compile time error

D) The code will run for some time and stop when the stack overflows

Answer: d

Explanation: Every function call is stored in the stack memory. In this case, there is no terminating
condition(base case). So, my_recursive_function() will be called continuously till the stack overflows and
there is no more space to store the function calls. At this point of time, the program will stop abruptly.

Q73) What is the base case for the following code?

void my_recursive_function(int n)

if(n == 0)

return;

printf("%d ",n);

my_recursive_function(n-1);

int main()
{

my_recursive_function(10);

return 0;

A)return

B) printf(“%d “, n)

C) if(n == 0)

D) my_recursive_function(n-1)

Answer: c

Explanation: For the base case, the recursive function is not called. So, “if(n == 0)” is the base case.

Q74) How many times is the recursive function called, when the following code is executed?

void my_recursive_function(int n)

if(n == 0)

return;

printf("%d ",n);

my_recursive_function(n-1);

int main()

my_recursive_function(10);

return 0;

A) 9
B) 10

C) 11

D) 12

Answer: C

Explanation: The recursive function is called 11 times.

Q75) Which of the following statements is true?

A) Recursion is always better than iteration

B) Recursion uses more memory compared to iteration

C) Recursion uses less memory compared to iteration

D) Iteration is always better and simpler than recursion

Answer: B

Explanation: Recursion uses more memory compared to iteration because every time the recursive
function is called, the function call is stored in stack.

Q76) What will be the output of the following code?

int cnt=0;

void my_recursive_function(int n)

if(n == 0)

return;

cnt++;

my_recursive_function(n/10);

int main()

{
my_recursive_function(123456789);

printf("%d",cnt);

return 0;

A)123456789

B)10

C)9

D)None

Ans: C

Sol: The program prints the number of digits in the number 123456789, which is 9.

Q77) What will be the output of the following code?

void my_recursive_function(int n)

if(n == 0)

printf("False");

return;

if(n == 1)

printf("True");

return;

}
if(n%2==0)

my_recursive_function(n/2);

else

printf("False");

return;

int main()

my_recursive_function(100);

return 0;

A)True

B)False

C) Compile time error

D) Run time error

Ans: B

Sol: The function checks if a number is a power of 2. Since 100 is not a power of 2, it prints false.

Q78) What is the output of the following code?

int cnt = 0;

void my_recursive_function(char *s, int i)

{
if(s[i] == '\0')

return;

if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')

cnt++;

my_recursive_function(s,i+1);

int main()

my_recursive_function("thisisrecursion",0);

printf("%d",cnt);

return 0;

A)6

B) 4

C) 9

D) 10

Ans: A

Sol: The function counts the number of vowels in a string. In this case the number is vowels is 6.

Q79) What is the output of the following code?

void my_recursive_function(int *arr, int val, int idx, int len)

if(idx == len)

printf("-1");
return ;

if(arr[idx] == val)

printf("%d",idx);

return;

my_recursive_function(arr,val,idx+1,len);

int main()

int array[10] = {7, 6, 4, 3, 2, 1, 9, 5, 0, 8};

int value = 2;

int len = 10;

my_recursive_function(array, value, 0, len);

return 0;

A)3

B)4

C)5

D)6

Ans: B

Sol: The program searches for a value in the given array and prints the index at which the value is found.
In this case, the program searches for value = 2. Since, the index of 2 is 4(0 based indexing), the program
prints 4.

Q80) function main()


{

integer a=5,b=7

switch(a)

case 5 :print “I am 5”

break

case b:print “I am not 5”

break

default:print “I am different”

A) I am 5

B) I am not 5

C) I am different

D) Error

Ans: D

You might also like