0

Test 2 - Programming and Data Structure | Computer Science(CS)

Description: GATE Previous year Topic Wise Questions and Answers | Programming and Data Structure
Number of Questions: 28
Created by:
Tags: Programming and Data Structures GATE CS
Attempted 0/28 Correct 0 Score 0

What is printed by the print statements in the program P1 assuming call by reference parameter passing?

Program P1(){
  x = 10;
  y = 3;
  func1(y, x, x);
  print x;
  print y;
}

func1(x, y, z) {
  y = y + 4;
  z = x + y + z;
}
  1. 10, 3

  2. 31, 3

  3. 27, 7

  4. None of the above


Correct Option: B
Explanation:

Here, we are passing the variables by call by reference. This means that the changes that we will make in the parameter would be reflected in the passed argument. Here, the first variable passed in the function func1 (i.e., y) points to the address of the variable x. Similarly, the second variable passed in the function func1 (i.e., x) points to the address of the variable y and the third variable passed in the function func1 (i.e., x) points to the address of the variable z.

So, we have y = y + 4 ⇒ y = 10 + 4 = 14 and z = x + y + z ⇒ z = 14 + 14 + 3 = 31 z will be returned to x. So, x = 31 and y will remain 3.

Consider the following declaration of a two-dimensional array in C: Char a[100][100] Assuming that the main memory is byte-addressable and that array is stored starting form memory address 0, the address of a [40] [50] is

  1. 4040

  2. 4050

  3. 5040

  4. 5050


Correct Option: B
Explanation:

The results returned by function under value-result and reference parameter passing conventions

  1. do not differ

  2. differ in the presence of loops

  3. differ in all cases

  4. may differ in the presence of exception


Correct Option: B
Explanation:

The results returned by function under value & reference parameter passing may differ in presence of loops.

Consider the following three functions:

Which of the above three functions are likely to cause problems with pointers?

  1. Only P3

  2. Only P1 and P3

  3. Only P1 and P2

  4. P1, P2 and P3


Correct Option: C
Explanation:

The goal of structured programming is to

  1. have well indented programs

  2. be able to infer the flow of control from the compiled code

  3. be able to infer the flow of control form the program text

  4. avoid the use of GOTO statements


Correct Option: D
Explanation:

Structured programming :- It is way of programming using the sub structure method, i.e splitting the programs into sub sections. Structured programming prevents confusing transfer of control of avoiding the use of GOTO statements.

The following numbers are inserted into an empty binary search tree in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the binary search tree (tree height is the maximum distance of a leaf node from the root)?

  1. 2

  2. 3

  3. 4

  4. 6


Correct Option: B
Explanation:

Given are 10, 1, 3, 5, 15, 12, 16

Consider the following program Program P2

If the language has dynamic scooping and parameters are passed by reference, what will be printed by the program?

  1. 10

  2. 11

  3. 3

  4. None of the above


Correct Option: D
Explanation:

n = 10 given but not passed to D. In D, n = 3 & W(n) increments by 1. So n = n + 1 = 4.

The best data structure to check whether an arithmetic expression has balanced parenthesis is a

  1. queue

  2. stack

  3. tree

  4. list


Correct Option: B
Explanation:

Balanced parenthesis in an equation is such that the no. of opening and closing parenthesis and in correct order should be there. We can check balancing using stack. When we get any opening parenthesis then we push that in the stack & if we get a closing one then we pop the stack. After the complete scanning of input string if stack is found empty then the arithmetic expression is balanced

Consider the following C function

int f(int n) {
  static int i = 1;
  if (n >= 5) return n;
  n = n + i;
  i++;
  return f(n);
}

The value returned by f(1) is

  1. 5

  2. 6

  3. 7

  4. 8


Correct Option: C
Explanation:

A circularly linked list is used to represent a Queue. A single variable p is used to access the Queue. To which node should p point such that both the operations enQueue and deQueue can be performed in constant time?

  1. Rear node

  2. Front node

  3. Not possible with a single pointer

  4. Node next to front


Correct Option: A
Explanation:

The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order into a max Heap. The resultant max Heap is


Correct Option: A
Explanation:

Consider the following C program segment:char p[20]; char *s = "string"; int length = strlen(s); int i; for (i = 0; i < length; i++) p[i] = s[length - i]; printf("%s",p);The output of the program is

  1. gnirts

  2. string

  3. gnirt

  4. no output is printed


Correct Option: D
Explanation:

Let us consider below line inside the for loop p[i] = s[length — i]; For i = 0, p[i] will be s[6 — 0] and s[6] is ‘\0′ So p[0] becomes ‘\0’. It doesn’t matter what comes in p[1], p[2]….. as P[0] will not change for i >0. Nothing is printed if we print a string with first character ‘\0′   

Consider the following C function void swap (int a, int b) { int temp; temp =a; a =b; b =temp; } In the order to exchange the values of two variables x and y .

  1. call swap (x,y)

  2. call swap (&x,&y)

  3. swap (x,y) cannot be used as it does not return any value

  4. swap (x,y) cannot be used as the parameters are passed by value


Correct Option: D
Explanation:

Here the function takes the arguments by value.

$\rightarrow$       Option (A) sends parameter by value but only the local variable a & b will be exchanged but not the actual variables x & y so incorrect. $\rightarrow$ Option (B) is incorrect sending address of x & y . $\rightarrow$ Option (C) swap (x,y) is usable there is no need to return. $\rightarrow$ Option (D) is the opposite statement of option (A), it says that the values are passed by value so won't swap so the option is correct.

Consider the following C program

The program computers

  1. x$\div$y, using repeated subtraction

  2. x mod y using repeated subtraction

  3. the greatest common divisor of x and y

  4. the least common multiple of x only


Correct Option: C
Explanation:

Consider the following program fragment for reversing the digits in a given integer to obtain a new integer.

Let n = d1 d2 ………… dm

int n, rev;
rev = 0;
while (n &lt; 0) {
  rev = rev * 10 + n % 10;
  n = n / 10;
}

The loop invariant condition at the end of the ith iteration is

  1. n = d1d2......dm−i and rev = dm dm−1......dm−i+1

  2. n = dm−i+1.....dm−1 dm or rev = dm−i .....d2d1

  3. n $\ne$ rev

  4. n = d1d2....dm or rev = dm......d2d1


Correct Option: A
Explanation:

What does the following C-statement declare?

  1. A function that takes an integer pointer as argument and returns an integer

  2. A function that takes an integer pointer as argument and returns an integer pointer

  3. A pointer to a function that takes an integer pointer as argument an returns

  4. A function that takes an integer pointer as argument returns a function pointer


Correct Option: C
Explanation:

What does the following algorithm approximate? (Assume m>1,

  1. log m

  2. m2

  3. m1/2

  4. m1/3


Correct Option: C
Explanation:

A single array A [1........MAXSIZE] is used to implement two stacks. The two stacks grow from opposite ends of the array. Variables top 1 and top 2 (top 1<top 2) point to the location of the topmost element in each of the stacks. If the space is to be used efficiently, the condition for “stack full” is

  1. (top 1= MAXSIZE/2) and (top 2 = MAXSIZE/.2+1)

  2. top 1+ top2=MAXSIZE

  3. (top 1= MAXSIZE/2) or (top2 = MAXSIZE)

  4. top 1= top 2−1


Correct Option: D
Explanation:

An Abstract Data type (ADT) is

  1. same as an abstract class

  2. a data type that cannot be instantiated

  3. a data type for which only the operations defined on it can be used, but none else

  4. all of the above


Correct Option: C
Explanation:

Abstract Data type :- It is defined as a user defined data type, specified by keyword 'abstract' & defines the variables & functions, these operations can only use the variables of this data type. So option (3) which says that Abstract data type for which only operations defined on it can be used is correct. Eg. stack data type Here operations defined are push & pop. So we can apply only these 2 operations on it.

Assume that the operators +, -, x are left associative and $\land$ is right associative .The order of precedence (from highest to lowest) is $\land$, x, +, -. The postfix expression corresponding to the infix expression a + b x c - d $\land$ e $\land$ f is

  1. abc x +def $\land$$\land$−

  2. abc x + de$\land$f$\land$

  3. ab +c x d−e$\land$f$\land$

  4. −+ a x bc$\land$$\land$def


Correct Option: A
Explanation:

Post order traversal of a given binary search tree, T produces the following sequence of keys 10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29

Which one of the following sequences of keys can be the result of an in order traversal of the tree T?

  1. 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95

  2. 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29

  3. 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95

  4. 95, 50, 60, 40, 27, 23, 22, 25, 10, 0, 15, 29


Correct Option: A
Explanation:

When we are given any no elements & even any order (preorder or post order) & we need to calculate in order, then in order is simply sorted sequence of the elements. Here 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95

A common property of logic programming languages and functional languages is

  1. both are procedural language

  2. both are based on$\lambda$−calculus

  3. both are declarative

  4. all of the above


Correct Option: D
Explanation:

$\lambda$-calculus" It provides the semantics for computation with functions so that properties of functional computation can be studied. Both the languages require declaration before use of any object. Both are procedural. So option (4) is correct. Both the languages are based on $\lambda$ calculus, procedural & declarative

Consider the following C program segment

The value returned by the function Do Something when a pointer to the proof of a non-empty tree is passed as argument is

  1. the number of leaf nodes in the tree

  2. the number of nodes in the tree

  3. the number of internal nodes in the tree

  4. the height of the tree


Correct Option: D
Explanation:

Value initialized by 0 If any root node has left child then it adds 1 to the value & move to left child & if any mode has right child also then also calculated the value using recursion & take maximum of both left & right value is taken. So we know that height is the largest distance between root node & leaf. So this program calculates heights.

A program P reads in 500 integers in the range (0, 100) representing the scores of 500 students. It then prints the frequency of each score above 50. What be the best way for P to store the frequencies?

  1. An array of 50 numbers

  2. An array of 100 numbers

  3. An array of 500 numbers

  4. A dynamically allocated array of 550 numbers


Correct Option: A
Explanation:

Here the no. readable are range 0 to 100 but the output of the program is interested in scores above 50 so there are 50 values (51 to 100) in this range. So only an array 50 integers required as we get any no. we increment the value stored at index.Array [x − 50] by

Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2.

  1. P-2, Q-3, R-4, S-1

  2. P-4, Q-3, R-2, S-1

  3. P-3, Q-4, R-1, S-2

  4. P-3, Q-4, R-2, S-1


Correct Option: D
Explanation:

Consider the following C-program:

void foo(int n, int sum) {
  int k = 0, j = 0;
  if (n == 0) return;
  k = n % 10;
  j = n / 10;
  sum = sum + k;
  foo(j, sum);
  printf("%d,", k);
}

int main() {
  int a = 2048, sum = 0;
  foo(a, sum);
  printf("%dn", sum);

  getchar();
}

What does the above program print?

  1. 8, 4, 0, 2, 14

  2. 8, 4, 0, 2, 0

  3. 2, 0, 4, 8, 14

  4. 2, 0, 4, 8, 0


Correct Option: D
Explanation:

Which of the following are essential features of an object-oriented programming language?

  1. Abstraction and encapsulation
  2. Strictly-typedness
  3. Type-safe property coupled with sub-type rule
  4. Polymorphism in the presence of inheritance
  1. 1 and 2 only

  2. 1 and 4 only

  3. 1, 2 and 4 only

  4. 1, 3 and 4 only


Correct Option: B
Explanation:

Object oriented programming languages necessarily have features like. Abstraction Encapsulation, inheritance with polymorphism but OOPL are also strongly-typed since there are restrictions on how operations involving values having different data types can be intermixed. Eg. two integers can be divided but one integer & one string can't.

Consider the following C-program:

double foo(double); /* Line 1 */
int main() {
  double da, db;
  // input da
  db = foo(da);
}

double foo(double a) {
  return a;
}

The above code complied without any error or warning. If Line 1 is deleted, the above code will show

  1. no compile warning or error

  2. some complier-warning not leading to unitended results

  3. Some complier-warning due to type-mismatch eventually leading to unitended results

  4. Complier errors


Correct Option: C
Explanation:

Here if line 1 which is prototype declaration of the function foo, in C compilation processes this would give a compile warning, due to type-mismatch. Since then compiler won't know what the return type of foo is. So unintended results may occur

- Hide questions