# GATE-CS-2004

• Last Updated : 11 Oct, 2021

 Question 1
The goal of structured programming is to:
 A have well indented programs B be able to infer the flow of control from the compiled code C be able to infer the flow of control from the program text D avoid the use of GOTO statements
GATE-CS-2004    Principles of Programming Languages
Discuss it

Question 1 Explanation:
The main goal of structured programming is to get an understanding about the flow of control in the given program text. In structure programming various control structures such as switch-case, if-then-else, while, etc. allows a programmer to decode the flow of the program easily.
 Question 2
Consider the following C function
```void swap (int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}
```
In order to exchange the values of two variables x and y.
 A Call swap (x, y) B Call swap (&x, &y) C swap(x,y) cannot be used as it does not return any value D swap(x,y) cannot be used as the parameters are passed by value
GATE-CS-2004    C Functions
Discuss it

Question 2 Explanation:
 Question 3
A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks grow from opposite ends of the array. Variables top1 and top2 (topl< 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
 A (top1 = MAXSIZE/2) and (top2 = MAXSIZE/2+1) B top1 + top2 = MAXSIZE C (top1= MAXSIZE/2) or (top2 = MAXSIZE) D top1= top2 -1
GATE-CS-2004
Discuss it

Question 3 Explanation:
 Question 4
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 (the height is the maximum distance of a leaf node from the root)?
 A 2 B 3 C 4 D 6
GATE-CS-2004
Discuss it

Question 4 Explanation:
 Question 5
The best data structure to check whether an arithmetic expression has balanced parentheses is a
 A queue B stack C tree D list
GATE-CS-2004
Discuss it

Question 5 Explanation:
 Question 6
Level order traversal of a rooted tree can be done by starting from the root and performing
 A preorder traversal B inorder traversal C depth first search D breadth first search
GATE-CS-2004    Top MCQs on Tree Traversal with Interview Question and Answers
Discuss it

Question 6 Explanation:
See this post for details
 Question 7
Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199) and the hash function x mod 10, which of the following statements are true?
```
1.    9679, 1989, 4199 hash to the same value
2.    1471, 6171 hash to the same value
3.    All elements hash to the same value
4.    Each element hashes to a different value ```
 A 1 only B 2 only C 1 and 2 only D 3 or 4
GATE-CS-2004
Discuss it

Question 7 Explanation:
The hash value for 9679, 1989, 4199 is 9 and hash value for 1471, 6171 is 1.
 Question 8
Which of the following grammar rules violate the requirements of an operator grammar ? P, Q, R are nonterminals, and r, s, t are terminals.
```1.    P → Q R
2.    P → Q s R
3.    P → ε
4.    P → Q t R r ```
 A 1 only B 1 and 3 only C 2 and 3 only D 3 and 4 only
GATE-CS-2004    Parsing and Syntax directed translation
Discuss it

Question 8 Explanation:
See Question 4 of http://www.geeksforgeeks.org/compilers-set-1/. Operator grammar should not have two or more variables in its production side by side, and should not have null productions.
 Question 9
Consider a program P that consists of two source modules M1 and M2 contained in two different files. If M1 contains a reference to a function defined in M2, the reference will be resolved at
GATE-CS-2004    C Misc
Discuss it

Question 9 Explanation:
Note: Static linking is done at link-time, dynamic linking or shared libraries are brought in only at runtime. (A) Edit-time : Function references can never be given/determined at edit-time or code-writing time. Function references are different from function names. Function names are used at edit-time and function references are determined at linker-time for static libraries or at runtime for dynamic libraries. (B) Compile-time : Compile time binding is done for functions present in the same file or module. (C) Link-time : Link time binding is done in the linker stage, where functions present in separate files or modules are referenced in the executable. (D) Load-time : Function referencing is not done at load-time. Hence, correct answer would be (C).   This solution is contributed by Vineet Purswani.
 Question 10

Consider the grammar rule E → E1 - E2 for arith­metic expressions. The code generated is targeted to a CPU having a single user register. The sub­traction operation requires the first operand to be in the register. If E1 and E2 do not have any com­mon sub expression, in order to get the shortest possible code

 A E1 should be evaluated first B E2 should be evaluated first C Evaluation of E1 and E2 should necessarily be interleaved D Order of evaluation of E1 and E2 is of no consequence
GATE-CS-2004    Code Generation and Optimization
Discuss it

Question 10 Explanation:

E -> E1 - E2 Given that E1 and E2 don't share any sub expression, most optimized usage of single user register for evaluation of this production rule would come only when E2 is evaluated before E1. This is because when we will have E1  evaluated in the register, E2 would have been already computed and stored at some memory location. Hence we could just use subtraction operation to take the user register as first operand, i.e. E1 and E2 value from its   memory location referenced using some index register or some other form according to the instruction. Hence correct answer should be (B) E2 should be evaluated first.

There are 90 questions to complete.
My Personal Notes arrow_drop_up