Table of contents
1.
Introduction
2.
2 Marks Questions
3.
1 Mark Questions
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Jun 25, 2025
Medium

Basic of Programming Language

Author
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

If you are an undergraduate or graduate student interested in pursuing a postgraduate degree in science or technology, you should be aware of the GATE exam. GATE is one of the most prestigious exams for engineering students. Many students aspire to score well in this exam to kick start their careers. In this article, we will be going through the questions and solutions of Basic of Programming Language from previous year's GATE papers.

This is the continuation of Basic of Programming Language Part-1. Here we will be solving the 2 Marks questions of Basic of Programming Language asked in GATE.

Also Read About, procedure call in compiler design

 

2 Marks Questions

1. Consider the following pseudo-code, where x and y are positive integers.

begin 

    q := 0 

    r := x 

   while r ≥ y do 

      begin 

      r := r - y 

      q := q + 1 

    end 

end

The post condition that needs to be satisfied after the program terminates is

(A) {r = qx + y ∧ r < y}

(B) {x = qy + r ∧ r < y}

(C) {y = qx + r ∧ 0 < r < y}

(D) { q + 1 0}

 

ANS - (B)

Solution - 

1) It initializes r as x.

2) It repeatedly subtracts y from r until r becomes

   smaller than y. For every subtraction, it 

   increments count q.

3) Finally, r contains the remainder, i.e., x%y and q contains

   ⌊x/y⌋

See below pseudo code with comments.

begin

   q := 0 // q is going to contain floor(x/y)

   r := x // r is going to contain x % y

 

// Repeatedly subtract y from x.

while r >= y do

   begin

      r := r – y

      q := q + 1

   end

end

 

2. Consider the C function given below.

int f(int j)

{

    static int i = 50;

    int k;

    if (i == j)

    {

        printf("something");

        k = f(i);

        return 0;

    }

    else return 0;

}

Which one of the following is TRUE?

(A) The function returns 0 for all values of j.

(B) The function prints the string something for all values of j.

(C) The function returns 0 when j = 50.

(D) The function will exhaust the runtime stack or run into an infinite loop when j = 50

 

ANS - (D)

Solution - When j is 50, the function would call itself again and again as neither i nor j is changed inside the recursion.

 

3. Consider the following function.

double f (double x) {

    if ( abs (x * x – 3) < 0. 01) return x;

    else return f (x / 2 + 1.5/x);

}

Give a value q (to 2 decimals) such that f(q) will return q:______.

 

ANS - 1.73

Solution - The main thing to note is the expression "abs(x*x – 3) < 0.01″ inside the if condition. The function would return x when x2 is close to 0 (smaller than 0.01), which means when x is close to the square root of 3. The square root of 3 is 1.732.
 

4.  What is the return value of f (p, p) if the value of p is initialised to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.

int f (int &x, int c) {

    c = c - 1;

    if (c==0) return 1;

    x = x + 1;

    return f(x,c) * x;

}

(A) 3024

(B) 6561

(C) 55440

(D) 161051

 

ANS - (B)

Solution - Since c is passed by value and x is passed by reference, all functions will have the exact copy of x but different copies of c.

f(5, 5) = f(x, 4)*x = f(x, 3)*x*x = f(x, 2)*x*x*x = f(x, 1)*x*x*x*x = 1*x*x*x*x = x^4

Since x is incremented in every function call, it becomes 9 after the f(x, 2) call. So the value of expression x^4 becomes 9^4, which is 6561.

 

5. Which of the following are true?

I. A programming language that does not permit global variables of any kind and has no nesting of procedures/functions but permits recursion can be implemented with static storage allocation

II. Multi-level access link (or display) arrangement is needed to arrange activation records only if the programming language being implemented has a nesting of procedures/functions

III. Recursion in programming languages cannot be implemented with dynamic storage allocation

IV. Nesting procedures/functions and recursion require a dynamic heap allocation scheme and cannot be implemented with a stack-based allocation scheme for activation records

V. Programming languages that permit a function to return a function as its result cannot be implemented with a stack-based storage allocation scheme for activation records

(A) II and V only

(B) I, III, and IV only

(C) I, II, and V only

(D) II, III, and V only
 

ANS - (A)

Solution - II. Is CORRECT. Programming languages that support nested subroutines also have a field in the call frame that points to the stack frame of the latest activation of the procedure that most closely encapsulates the callee, i.e., the immediate scope of the callee. This is called an access link or static link (as it keeps track of static nesting during dynamic and recursive calls) and provides the routine (as well as any other routines it may invoke) access to the local data of its encapsulating routines at every nesting level.

V. Is CORRECT. In stack-based allocation schemes, once a function has returned, it is removed from the function call stack. Therefore returning a function from a function doesn't look possible.

 

6.  The following program fragment is written in a programming language that allows global variables and does not allow nested declarations of functions.

global int i = 100, j = 5;

void P(x) {

    int i = 10;

    print(x + 10);

    i = 200;

    j = 20;

    print (x);

}

main() {

    P(i + j);

}

If the programming language uses static scoping and call by need parameter passing mechanism, the values printed by the above program are

(A) 115, 220

(B) 25, 220

(C) 25, 15

(D) 115, 105

 

ANS - (A)

Solution - global int i = 100, j = 5;

void P(x) // x = i + j

{

int i = 10;

print(x + 10);// print (100+5+10) = 115

i = 200;

j = 20;

print(x); // print (200+20) = 220.

// i and j would be changed as they are global variables

}

main()

{

P(i + j);

}

 

7. The following program fragment is written in a programming language that allows global variables and does not allow nested declarations of functions.

global int i = 100, j = 5;

void P(x) {

    int i = 10;

    print(x + 10);

    i = 200;

    j = 20;

    print (x);

}

main() {

    P(i + j);

}

If the programming language uses dynamic scoping and call by name parameter passing mechanism, the values printed by the above program are
 

(A) 115, 220

(B) 25, 220

(C) 25, 15

(D) 115, 105

 

ANS - (A)

Solution - 

global int i = 100, j = 5;

void P(x) // x = i + j

{

int i = 10;

print(x + 10);// print (100+5+10) = 115

i = 200;

j = 20;

print(x); // print (200+20) = 220.

// i and j would be changed as they are global variables

}

main()

{

P(i + j);

}
 

8. Consider the following program.

Program P2

      var n:int;

      procedure W(var x:int)

      begin

          x=x+1;

          print x;

      end
 

      procedure D

      begin

          var n:int;

          n=3;

          W(n);

      end

begin \\begin P2

      n = 10;

      D;

end

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

(A) 10

(B) 11

(C) 3

(D) None of the above

 

ANS - (D)

Solution - In static scoping or compile-time scoping, the free variables (variables used in a function that are neither local variables nor parameters of that function) are referred to as global variables because, at compile, only global variables are available.

In dynamic scoping or runtime scoping, the free variables are referred to as the variables in the most recent frame of the function call stack. In the given code in the function call of procedure W, the local variable x is printed, i.e., 4. Under dynamic scoping, if x would not have been there in procedure W, then we would refer to x of the function in the function call stack, i.e., procedure D and the main function, but since x is a local variable, not a free variable we referred to the local variable hence four will be printed.

 

9.  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;

}

(A) 10, 3

(B) 31, 3

(C) 27, 7

(D) None of the above
 

ANS - (B)

Solution - Here, we are passing the variables by call by reference. This means that the changes we will make in the parameter would be reflected in the given 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. 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.

 

10. A certain processor supports only the immediate and the direct addressing modes. Which of the following programming language features cannot be implemented on this processor?

a) Pointers

b) Arrays

c) Records

d) Recursive procedures with local variables

(A) Only a

(B) a and b

(C) c and d

(D) a, b, c, and d

ANS - (D)
 

11. Consider the following program in a language that has dynamic scoping:

var x: real;

procedure show;

         begin print(x); end;

procedure small;

         var x: real;

             begin x: = 0.125; show; end;

begin x:=0.25;

         show; small

         end;

Then the output of the program is:

(A) 0.125 0.125

(B) 0.25 0.25

(C) 0.25 0.125

(D) 0.125 0.25

ANS - (C)
 

12. Given the programming constructs:

(i) assignment

(ii) for loops where the loop parameter cannot be changed within the loop

(iii) if-then-else

(iv) forward, go to

(v) arbitrary go to

(vi) non-recursive procedure call

(vii) recursive procedure/function call

(viii) repeat loop,

which constructs will you not include in a programming language such that it should be possible to program the terminates (i.e., halting) function in the same programming language?
 

(A) ii), iii), iv)

(B) v), vii), viii)

(C) vi), vii), viii)

(D) iii), v), viii)

 

ANS - (B)
 

13. Faster access to non-local variables is achieved using an array of pointers to activation records called a

(A) stack

(B) heap

(C) display

(D) activation tree
 

ANS - (D)

Solution - The faster access to non-local variables is achieved using an array of pointers to activation records, called an activation tree.
 

14. Given the following Pascal-like program segment:

Procedure A;

     x,y:integer;

     Procedure B;

            x,z:real;

            S1

     end B;

     Procedure C;

            i:integer;

            S2;

     end C;

end A;

The variables accessible in S1 and S2 are

(A) x of A, y, x of B, and z in S1 and x of B, y, and i in S2

(B) x of B, y and z in S1 and x of B, i and z in S2

(C) x of B, z and y in S1 and x of A, i and y in S2

(D) None of the above
 

ANS - (C)


15. The correct matching for the following pairs is

List - I

(A) Activation record

(B) Location counter

(C) Reference counts

(D) Address relocation

List - II

(1) Linking loader

(2) Garbage collection

(3) Subroutine call

(4) Assembler

 

(A) A-3, B-4, C-1, D-2

(B) A-4, B-3, C-1, D-2

(C) A-4, B-3, C-2, D-1

(D) A-3, B-4, C-2, D-1
 

ANS - (D)
 

16. In which one of the following cases is it possible to obtain different results for call-by-reference and call-by-name parameter passing methods?

(A)Passing a constant value as a parameter

(B)Passing the address of an array as a parameter

(C)Passing an array element as a parameter

(D)Passing an array

 

ANS - (C)

Solution - 

Passing an array element as a parameter is the answer.

Consider this function call.

{

    ....

    a[] = {0,1,2,3,4};

    i = 0;

    fun(a[i]);

    print a[0];

}

fun(int x)

{

    int i = 1;

    x = 8;

}

Output:

  • call-by-reference:8
  • call-by-name:0

In Call-by-name, each occurrence of the formal parameter is replaced by the actual argument text. So, the function fun will be executed like:

{

   int i = 1;

   a[i] = 8; //a[1] is changed to 8 and not a[0]

}

1 Mark Questions

  1. What is printed by the following ANSI C program?

  #include<stdio.h> 

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

   char a = ‘p’;

   char b = ‘x’;

   char c = (a&b) + ‘*’;

   char d = (a|b) – ‘-’;

   char e = (a^b) + ‘+’;

   printf(“%c %c %c\n”, c, d, e);

   return 0;

}

ASCII encoding for relevant characters is given below :
 

ANS - zKS

Solution -

char a = ‘P’

char b = ‘x’

As per precedence of operators, () evaluated before +/-

Note that &,^, and | are bitwise operators and Addition/Subtraction of characters applied to their ASCII values.


a = ‘P’ ===> a = Ascii value of (‘P’) = 65+15 = 80 ==> a = (0101 0000)2

b = ‘x’ ===> b = Ascii value of (‘x’) = 97+23 = 120 ==> b = (0111 1000)2

a & b = (0101 0000)2

[ apply & logic on corresponding bits ] = (80)10

‘*’ = 42 = (0010 1010)2

a&b + ‘*’ = (0111 1010)2 = 64+32+16+8+2 = 122 [ add corresponding bits ], [ simply 80+42 = 122 ]

print character ( 122 ) = small ‘z’


a | b = (0111 1000)2 [ apply | logic on corresponding bits ] = (120)10

‘-’ = 45

a|b - ‘-’ = 120-45 = 75 = 65 + 10

print character ( 75 ) = Capital ‘K’


a ^ b =(0010 1000)2

2 [ apply ^ logic on corresponding bits ] = (40)10

‘+’ = 43

a^b + ‘+’ = 40+43 = 83 = 65 + 18

print character ( 75 ) = Capital ‘S’

 

2. Consider the following ANSI C program:

int main() {

Integer x;

return 0;

}

Which one of the following phases in a seven-phase C compiler will throw an error?  
 

ANS -  Semantic analyzer
 

3. Consider the following C function.

int fun ( int n ) {

  int x = 1, k ;

  if ( n == 1) return x ;

   for( k = 1 ; k < n ; ++ k )

    x = x + fun( k ) * fun( n - k ) ;

   return x ;

}

The return value of fun (5) is ________.
 

ANS - 51

Solution -

fun(5) = 1 + fun(1) * fun(4) + fun(2) * fun(3) + 

             fun(3) * fun(2) + fun(4) * fun(1)

       = 1 + 2*[fun(1)*fun(4) + fun(2)*fun(3)]

Substituting fun(1) = 1

       = 1 + 2*[fun(4) + fun(2)*fun(3)]

Calculating fun(2), fun(3) and fun(4)

fun(2) = 1 + fun(1)*fun(1) = 1 + 1*1 = 2

fun(3) = 1 + 2*fun(1)*fun(2) = 1 + 2*1*2 = 5

fun(4) = 1 + 2*fun(1)*fun(3) + fun(2)*fun(2)

       = 1 + 2*1*5 + 2*2 = 15

Substituting values of fun(2), fun(3) and fun(4)

fun(5) = 1 + 2*[15 + 2*5] = 51

 

4. Let A be a square matrix size n×n. Consider the following pseudocode. What is the expected output?

C = 100; 

for i = 0 to n do 

for j = 1 to n do 

 { 

      Temp = A[ i ][ j ] + C ; 

      A[ i ][ j ] = A[ j ][ i ] ; 

      A[ j ][ i ] = Temp - C ; 

 } 

 for i = 0 to n do 

 for j = 1 to n do 

 output(A[ i ][ j ]);


ANS - The matrix A itself.

Solution - If we take a look at the inner statements of the first loop, we can notice that the statements swap A[i][j] and A[j][i] for all i and j. Since the loop runs for all elements, every element A[l][m] would be swapped twice, once for i = l and j = m and then for i = m and j = l. Swapping twice means the matrix doesn't change.
 

5. Which of the following statements are CORRECT?

1) Static allocation of all data areas by a compiler makes it impossible to implement recursion.

2) Automatic garbage collection is essential to implement recursion.

3) Dynamic allocation of activation records is essential to implement recursion.

4) Both heap and stack are essential to implement recursion.


ANS - 1 and 3 only

Solution - 

Statement 1 is true because the dynamic memory allocation is required for the function call stack as the number of calls is not known in advance for recursive functions.

Statement 3 is true as the number of calls or activation records is not known in advance for recursive functions.


6. Which one of the following is NOT performed during compilation?

(A) Dynamic memory allocation

(B) Type checking

(C) Symbol table management

(D) Inline expansion

 

ANS - Dynamic memory allocation

Solution - Dynamic memory allocation happens at run time only. The compiler only compiles instructions for dynamic memory allocation like malloc(), calloc().
 

7. Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3. If n is large, which one of the following statements is most likely to set p correctly?

(A) p = n * (n-1) * (n-2) / 6;

(B) p = n * (n-1) / 2 * (n-2) / 3;

(C) p = n * (n-1) / 3 * (n-2) / 2;

(D) p = n * (n-1) * (n-2) / 6.0;

 

ANS - (B)

Solution - As n is large, the product n*(n-1)*(n-2) will go out of the range(overflow), and it will return a value different from what is expected. Therefore, options (A) and (D) are eliminated.

So we consider a shorter product n*(n-1).

n*(n-1) is always an even number. So the subexpression" n * (n-1) / 2" in option B would always produce an integer, which means no precision loss in this subexpression. And when we consider" n*(n-1)/2*(n-2) ", it will always give a number that is a multiple of 3. So dividing it by three won't have any loss.

 

8. Consider the function func shown below:

int func(int num) 

         int count = 0;

         while(num)

         {

                count++;

                num >>= 1;

         }

  return (count);

}

The value returned by func(435) is _________.

 

ANS - 9

Solution - The function mainly returns the position of the Most significant bit in the binary representation of n. The MSD in the binary representation of 435 is the 9th bit.
 

9. Consider the following program in C language:

#include < stdio.h >

main()

{

int i;

int *pi = &i;

scanf("%d", pi);

printf("%d\n", i + 5);

}
 

Which one of the following statements is TRUE? 

(A) Compilation fails. 

(B) Execution results in a runtime error. 

(C) On execution, the value printed is five more than the address of variable i. 

(D) On execution, the value printed is five more than the integer value entered.

 

ANS - (D)

Solution - The program has no problem as pi points to a valid location. 

Also, in scanf() we pass the address of a variable, and pi is an address.

 

10. Which languages necessarily need heap allocation in the runtime environment?

(A) Those that support recursion

(B) Those that use dynamic scoping

(C) Those that allow dynamic data structures

(D) Those that use global variables
 

ANS - (C)

Solution - Heap allocation is needed for dynamic data structures like trees, linked lists, etc.

 

11. A common property of logic programming languages and functional languages is:

(A) both are procedural languages

(B) both are based on λ-calculus

(C) both are declarative

(D) both use Horn-clauses
 

ANS - (C)
 

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


Group 1

P. Functional

Q. Logic

R. Object-oriented

S. Imperative

Group 2

1. Command-based, procedural

2. Imperative, abstract data types

3. Side-effect free, declarative, expression Evaluation

4. Declarative, clausal representation, theorem proving
 

(A) P-2, Q-3, R-4, S-1

(B) P-4, Q-3, R-2, S-1

(C) P-3, Q-4, R-1, S-2

(D) P-3, Q-4, R-2, S-1

 

ANS - (D)

Solution - P: Functional Programming is declarative, involves

expression evaluation, & side effects free.

Q: Logic is also declarative but involves theorem proving.

R: Object-oriented is an imperative statement based & has abstract

(general) data types.

S: Imperative: The programs are made by giving commands & follows

definite procedure & sequence.
 

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

ANS - (C)

Solution - The main goal of structured programming is to understand the flow of control in the given program text. In structure programming, various control structures such as switch-case, if-then-else, while, etc., allow a programmer to decode the flow of the program quickly.
 

14. Which of the following statements is FALSE?

(A) In a statically typed language, each variable in a program has a fixed type

(B) In un-typed languages, values do not have any types

(C) In dynamically typed languages, variables have no types

(D) In all statically typed languages, each variable in a program is associated with values of only a single type during the execution of the program

 

ANS - (C)

Solution - Dynamically typed languages deduce types of values and bind them to the variables storing those values. Hence, values sure have fixed types, but variables don't have fixed types bound to them. Although we can say that binding a type to a variable according to values makes them typeful, they don't have one fixed type. This statement has some ambiguity. [TRUE/FALSE].

 

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

(A) Do not differ

(B) Differ in the presence of loops

(C) Differ in all cases

(D) May differ in the presence of exceptions
 

ANS - (D)

Solution - The result is updated since the updated values are returned to the original variable. In call by reference, any change in the variable reflects immediately.
 

16. Heap allocation is required for languages.

(A) use dynamic scope rules

(B) support dynamic data structures

(C) support recursion

(D) support recursion and dynamic data structures

 

ANS - (B)

Solution - Heap allocation is required for the languages which support dynamic data structure.
 

17. What are x and y in the following macro definition?

macro Add x,y

Load y

Mul x

Store y

end macro

ANS - (D) Formal Parameters
 

18. An unrestricted use of the "goto" statement is harmful because

ANS - (a) it makes it more challenging to verify programs

Solution - goto in no way can increase the program's running time or memory requirement. It also doesn't contribute to more extended machine code. But the use of goto can result in unstructured code, and there can be blocks with multiple entries and exit points which can cause a nightmare for program verification. 

 

19. Indicate the following statement true or false:

A programming language not supporting either recursion or pointer type does not need the support of dynamic memory allocation.
 

ANS - FALSE
 

20. Indicate the following statement true or false:

Although C does not support call-by-name parameter passing, C can correctly simulate the effect.
 

ANS - FALSE


Must Read: Difference Between Compiler and Assembler

Frequently Asked Questions

  1. What is the eligibility criteria for the GATE Exam?
    The candidate should have done their Bachelors in Science or Engineering.
     
  2. What is GATE?
    GATE, or Graduate Aptitude Test in Engineering, is an All-India Examination conducted jointly by the Indian Institute of Science (IISc), Bangalore, and the seven Indian Institutes of Technology (IITs).
     
  3. What are the questions asked in GATE?
    Two types of MCQs: MCQs - 1 mark for each correct answer; 1/3 mark will be deducted for every wrong answer. MCQs - 2 marks for each correct answer; 2/3 marks for every incorrect response will be deducted. There are no negative markings for Numerical Answer Type (NAT) questions.
     
  4. Are there any restrictions or age limits to applying for the GATE Exams 2023?
    No, there are no restrictions on the number of times.
     
  5. Is a 3rd-year B.A./B.Com./B.Sc. student eligible to appear for GATE 2021?
    Yes, any undergraduate student currently in the 3rd year or higher years of any government-approved program in Engineering / Technology / Architecture / Arts / Commerce / Science is eligible to appear for GATE 2021. 

 

Conclusion

In this article, we have extensively discussed the approach and solutions to the past year's questions of Basic Programming Language asked in GATE. We hope this blog has helped you enhance your knowledge of the concepts and process of the questions.

If you want to learn more, check out our articles on Introduction to GATER Programming Language, How to prepare in the Last 10 days to score high in GATE?Basic of Programming Language Part-1.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. 

Enrol in our courses and refer to the mock test and problems available.

Take a look at the interview experiences and interview bundle for placement preparations.

 

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass