PRogramming

Question 1
Program PARAM (input, output);
 var m, n : integer;
 procedure P (var, x, y : integer);
    var m : integer;
    begin
      m : = 1;
      x : = y + 1
    end;
 procedure Q (x:integer; vary : integer);
    begin
      x:=y+1;
    end;
 begin
    m:=0; P(m,m); write (m);
    n:=0; Q(n*1,n); write (n)
 end 

The value of n, output by the program PARAM is:

A
0, because n is the actual parameter corresponding to x in procedure Q.
B
0, because n is the actual parameter to y in procedure Q.
C
1, because n is the actual parameter corresponding to x in procedure Q.
D
1, because n is the actual parameter corresponding to y in procedure Q.
E
none of the above
Question 1 Explanation: 
0, because n is just passed to formal parameters of Q and no modification in global n.
Question 2
Program PARAM (input, output);
 var m, n : integer;
 procedure P (var, x, y : integer);
    var m : integer;
    begin
      m : = 1;
      x : = y + 1
    end;
 procedure Q (x:integer; vary : integer);
    begin
      x:=y+1;
    end;
 begin
    m:=0; P(m,m); write (m);
    n:=0; Q(n*1,n); write (n)
 end 

What is the scope of m declared in the main program?

A
PARAM, P, Q
B
PARAM, P
C
PARAM, Q
D
P, Q
E
none of the above
Question 2 Explanation: 
Since m is defined global it is visible inside all the procedures.
Question 3

What does the following code do?

 var a, b : integer;
 begin
    a:=a+b;
    b:=a-b;
    a:=a-b
 end; 
A
exchanges a and b
B
doubles a and stores in b
C
doubles b and stores in a
D
leaves a and b unchanged
E
none of the above
Question 3 Explanation: 
Exchanges a and b.
Let us consider a=5; b=2
a := a+b = 5+2 = 7
b := a-b = 7-2 = 5
a := a-b = 7-5 = 2
O/P: a=2; b=5
Question 4
Program PARAM (input, output);
 var m, n : integer;
 procedure P (var, x, y : integer);
    var m : integer;
    begin
      m : = 1;
      x : = y + 1
    end;
 procedure Q (x:integer; vary : integer);
    begin
      x:=y+1;
    end;
 begin
    m:=0; P(m,m); write (m);
    n:=0; Q(n*1,n); write (n)
 end 

The value of m, output by the program PARAM is:

A
1, because m is a local variable in P
B
0, because m is the actual parameter that corresponds to the formal parameter in p
C
0, because both x and y are just reference to m, and y has the value 0
D
1, because both x and y are just references to m which gets modified in procedure P
E
none of the above
Question 4 Explanation: 
0, because global m is not modified, m is just passed to formal argument of P.
Question 5

An unrestricted use of the “goto” statement is harmful because

A
it makes it more difficult to verify programs
B
it increases the running time of the programs
C
it increases the memory required for the programs
D
it results in the compiler generating longer machine code
Question 5 Explanation: 
If we use "goto" statements then it leads to structural decomposition of code then it is difficult to verify the programs.
Question 6

What is the value of X printed by the following program?

 program COMPUTE (input, output);
    var
                  X:integer;
    procedure FIND (X:real);
                  begin
                  X:=sqrt(X);
                  end;
   begin
                  X:=2
                  Find(X)
                  Writeln(X)
   end 
A
2
B
√2
C
Run time error
D
None of the above
Question 6 Explanation: 
Since nothing is said in question. So we will assume by default call by value.
X in the procedure FIND is a local variable. No change will be reflected in global variable X.
Question 7

What value would the following function return for the input x=95?

                 Function fun (x:integer):integer;
      Begin
                 If x > 100 then fun = x - 10
                 Else fun = fun(fun(x + 11))
      End;  
A
89
B
90
C
91
D
92
Question 7 Explanation: 
Value returned by
fun(95) = fun(fun(106))
= fun(96)
= fun(fun(107))
= fun(97)
= fun(fun(108))
= fun(98)
= fun(fun(109))
= fun(99)
= fun(110)
= fun(100)
= fun(fun(111))
= fun(101)
= 91
Question 8

What is the result of the following program?

program side-effect (input, output);
    var x, result: integer;
    function f (var x:integer):integer;
    begin
        x:x+1;f:=x;
    end;
begin
    x:=5;
    result:=f(x)*f(x);
    writeln(result);
end; 
A
5
B
25
C
36
D
42
Question 8 Explanation: 
If it is call by reference then answer is 42.
If it is call by value then answer is 36.
Question 9

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), (vii), (viii)
Question 9 Explanation: 
Arbitrary goto, recursive call and repeat loop may enter infinite loop and the program might never terminate.
Question 10

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               //beginP2 
                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
Question 10 Explanation: 
n=3
W(n)=W(3)
Procedure W(var x; int)
begin
x = x+1 = 3+1 = 4
Print x → Print x=4
end
Question 11

Consider the following program written in pseudo-code. Assume that x and y are integers.

Count (x, y)  {
    if (y != 1)  {
        if (x != 1)  {
            print("*") ;
            Count (x/2, y) ;
        }
        else  {
             y = y - 1 ; 
             Count (1024, y) ; 
        }
    }
}

The number of times that the print statement is executed by the call Count(1024, 1024) is ______.

A
10230
B
10231
C
10232
D
10233
Question 11 Explanation: 
#include
int count=0;
Count(x,y){
if(y!=1){
if(x!=1){
printf("*");
count = count +1;
Count(x/2,y);
}
else{
y=y-1;
Count(1024,y);
}
}
}
void main()
{
Count(1024,1024);
printf("\n%d\n",count);
}


Count ( ) is called recursively for every (y = 1023) & for every y, Count ( ) is called (x = 10) times = 1023 × 10 = 10230
Question 12

Consider the following C code. Assume that unsigned long int type length is 64 bits.

        unsigned long int fun(unsigned long int n)  { 
            unsigned long int i, j, j=0, sum = 0; 
            for (i = n; i > 1; i = i/2) j++; 
            for ( ; j > 1; j = j/2) sum++; 
            return sum; 
        } 

The value returned when we call fun with the input 240 is

A
4
B
5
C
6
D
40
Question 12 Explanation: 
Since 240 is the input, so first loop will make j = 40.
Next for loop will divide j value (which is 40) by 2, each time until j>1.
j loop starts:
j=40 & sum=1
j=20 & sum=2
j=10 & sum=3
j=5 & sum=4
j=2 & sum=5
j=1 & break
So, sum = 5.
Question 13

Consider the following C program:

#include<stdio.h> 

void fun1(char *s1, char *s2)  { 
     char *tmp;  
     tmp = s1; 
     s1 = s2; 
     s2 = tmp; 
} 

void fun2(char **s2, char **s2)  { 
     char *tmp; 
     tmp = *s1; 
     *s1 = *s2; 
     *s2 = tmp; 
}

int main ()  { 
     char *str1 = "Hi", *str2 = "Bye"; 
     fun1(str1, str2);     printf("%s %s", str1, str2); 
     fun2(&str1, &str2);   printf("%s %s", str1, str2); 
     return 0; 
} 

The output of the program above is

A
Hi Bye Bye Hi
B
Hi Bye Hi Bye
C
Bye Hi Hi Bye
D
Bye Hi Bye Hi
Question 13 Explanation: 
The first call to the function ‘func1(str1, str2);’ is call by value.
Hence, any change in the formal parameters are NOT reflected in actual parameters.
Hence, str1 points at “hi” and str2 points at “bye”.
The second call to the function ‘func2(&str1, &str2);’ is call by reference.
Hence, any change in formal parameters are reflected in actual parameters.
Hence, str1 now points at “bye” and str2 points at “hi”.
Hence answer is “hi bye bye hi”.
Question 14

Consider the following C program.

#include <stdio.h>
struct Outnode { 
    char x, y, z ; 
};

int main () { 
    struct Outnode p = {'1', '0', 'a'+2} ; 
    struct Outnode *q = &p ; 
    printf ("%c, %c", *((char*)q+1), *((char*)q+2)) ; 
    return 0 ; 
} 

The output of this program is

A
0, c
B
0, a+2
C
‘0’, ‘a+2’
D
‘0’, ‘c’
Question 14 Explanation: 
char x = ‘a’+2;
The x variable here stores a character ‘c’ in it.
Because +2 will increment ascii value of a from 92 to 95.
Hence the structure p contains 3 character values and they are ‘1’, ‘0’, and ‘c’.
q is a pointer pointing to structure p.
Hence q is pointing to ‘1’, q+1 pointing to ‘0’ and q+2 pointing to ‘c’.
Option d cannot be correct, as though they are characters, printf will not print them in single quotes.
Question 15
Consider the following C program:
#include <stdio.h>
int counter = 0;
int calc (int a, int b) {
int c;
counter++;
if (b==3) return (a*a*a) ;
else {
c = calc (a, b/3) ;
return (c*c*c) ;
}
}
int main () {
calc (4, 81);
printf ("%d", counter) ;
}
The output of this program is ______.
A
4
B
5
C
6
D
7
Question 15 Explanation: 
Question 16

Consider the following C program:

#include <stdio.h>
typedef struct 
{
    char *a;
    char *b;
} t;
void f1(t s);
void f2(t *p);
main()
{
    static t s = {"A", "B"};
    printf ("%s %sn", s.a, s.b);
    f1(s);
    printf ("%s %sn", s.a, s.b);
    f2(&s);
}
void f1(t s)
{
    s.a = "U";
    s.b = "V";
    printf ("%s %sn", s.a, s.b);
    return;
}
void f2(t *p)
{
    p -> a  = "V";
    p -> b = "W";
    printf("%s %sn", p -> a, p -> b);
    return;
} 
What is the output generated by the program?

A
AB
UV
VW
VW
B
AB
UV
AB
VW
C
AB
UV
UV
VW
D
AB
UV
VW
UV
Question 16 Explanation: 
→ First print AB.
→ f1 is call by value. The changes applicable only for local from f1. UV is printed.
→ Back in main( ), AB is printed.
→ Then in f2, VW is printed.
Hence, answer is (B).
Question 17

Choose the correct option to fill the ?1 and ?2 so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line character.

#include 
void wrt_it (void);
int main (void)
{
    printf("Enter Text"); 
    printf ("n");
    wrt_ it();
    printf ("n");
    return 0;
}
void wrt_it (void)
{
    int c;
    if (?1)
        wrt_it();
    ?2
} 
A
?1 is getchar() ! = ‘\n’
?2 is getchar(c);
B
?1 is (c = getchar()); ! = ‘\n’
?2 is getchar(c);
C
?1 is c! = ‘\n’
?2 is putchar(c);
D
?1 is (c = getchar()) ! = ‘\n’
?2 is putchar(c);
Question 17 Explanation: 
getchar( ) = reads a single character at a time from the stdin.
putchar( ) = writes a character specified by the argument to stdout.
As getchar( ) and putchar( ), both are needed to read the string and prints its reverse and only option (D) contains both the function. (D) is the answer.
Now coming to the code, wrt_id(void) is calling itself recursively. When \n is encountered, putchar( ) gets executed and prints the last character and then the function returns to its previous call and prints last 2nd character and so on.
Question 18

What is the output of the following program?

#include <stdio.h>
int funcf (int x);
int funcg (int y);

main()
{
    int x = 5, y = 10, count;
    for (count = 1; count <= 2; ++count)
    {
        y += funcf(x) + funcg(x);
        printf ("%d ", y);
    }
}

funcf(int x)
{
    int y;
    y = funcg(x);
    return (y);
}

funcg(int x)
{
    static int y = 10;
    y += 1;
    return (y+x);
}
A
43 80
B
42 74
C
33 37
D
32 32
Question 18 Explanation: 
In first iteration:
In first case of funcf, which in turn calls funcg, y becomes 11 and it returns 5+11 = 16.
In second call of funcg, y becomes 12 and it returns 5+12 = 17.
So, in main y is incremented by 16+17 = 33 to become 10+33 = 43.
In second iteration:
y will be incremented by 18+19 = 37 to give 43+37 = 80.
Question 19

Consider the following C program which is supposed to compute the transpose of a given 4 x 4 matrix M. Note that, there is an X in the program which indicates some missing statements. Choose the correct option to replace X in the program.

#include<stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
    int i, j, t;
    for (i = 0; i < 4; ++i)
    {
        X
    }
    for (1 = 0; i < 4; ++i)
        for (j = 0; j < 4; ++j)
            printf ("%d", M[i][j]);
}
A
for(j = 0; j < 4; ++j){
t = M[i][j];
M[i][j] = M[j][i];
M[j][i] = t;
}
B
for(j = 0; j < 4; ++j){
M[i][j] = t;
t = M[j][i];
M[j][i] = M[i][j];
}
C
for(j = i; j < 4; ++j){
t = M[i][j];
M[i][j] = M[j][i];
M[j][i] = t;
}
D
for(j = i; j < 4; ++j){
M[i][j] = t;
t = M[j][i];
M[j][i] = M[i][j];
}
Question 19 Explanation: 
To compute transpose 'j' needs to be started with 'i'. So, A and B are wrong.
In (D) , given statements is wrong as temporary variable needs to be assigned some value and not vice-versa.
Question 20

A program attempts to generate as many permutations as possible of the string, 'abcd' by pushing the characters a, b, c, d in the same order onto a stack, but it may pop off the top character at any time. Which one of the following strings CANNOT be generated using this program?

A
abcd
B
dcba
C
abad
D
cabd
Question 20 Explanation: 
A) push 'a' and pop 'a', push 'b' and pop 'b', push 'c' and pop 'c', and finally push 'd' and pop 'd'. Sequence of popped elements will come to abcd.
B) First push abcd, and after that pop one by one. Sequence of popped elements will come to dcba.
C) push abc, and after that pop one by one. Sequence of popped elements will come to cba. Now push 'd' and pop 'd', final sequence comes to cbad.
D) This sequence is not possible because 'a' cannot be popped before 'b' anyhow.
Question 21

Let x be an integer which can take a value of 0 or 1. The statement if(x = =0) x = 1; else x = 0; is equivalent to which one of the following?

A
x = 1 + x;
B
x = 1 - x;
C
x = x - 1;
D
x = 1 % x;
Question 21 Explanation: 
x = 1 - x
For x = 0, it gives 1.
For x = 1, it gives 0.
Question 22

What is the output printed by the following program?

#include
int f(int n, int k)
{
    if (n == 0)
        return 0;
    else if (n % 2)
        return f(n/2, 2*k) + k;
    else return f(n/2, 2*k) - k;
}
int main ()
{
    printf("%d", f(20, 1));
    return 0;
}
A
5
B
8
C
9
D
20
Question 22 Explanation: 

Hence, 9 is the answer.
Question 23

Let a be an array containing n integers in increasing order. The following algorithm determines whether there are two distinct numbers in the array whose difference is a specified number S > 0.

i = 0;
j = 1;
while (j < n )
{
    if (E) j++;
    else if (a[j] - a[i] == S) break;
    else i++;
}
if (j < n)
    printf("yes")
else
   printf ("no"); 

Choose the correct expression for E.

A
a[j] – a[i] > S
B
a[j] – a[i] < S
C
a[i] – a[j] < S
D
a[i] – a[j] > S
Question 23 Explanation: 
For some 'i' if we find that difference of (A[j] - A[i] < S) we increment 'j' to make this difference wider so that it becomes equal to S.
If at times difference becomes greater than S we know that it won't reduce further for same 'i' and so we increment the 'i'.
Question 24

The following C function takes a singly-linked list of integers as a parameter and rearranges the elements of the list. The list is represented as pointer to a structure. The function is called with the list containing the integers 1, 2, 3, 4, 5, 6, 7 in the given order. What will be the contents of the list after the function completes execution?

struct node {
    int value; 
    struct node *next;
);
void rearrange (struct node *list) 
{
    struct node *p, *q;
    int temp;
    if (!list || !list -> next) 
        return;
    p = list; 
    q = list -> next;
    while (q) 
    {
        temp = p -> value;
        p -> value = q -> value;
        q -> value = temp;
        p = q -> next;
        q = p ? p -> next : 0;
    }
} 
A
1, 2, 3, 4, 5, 6, 7
B
2, 1, 4, 3, 6, 5, 7
C
1, 3, 2, 5, 4, 7, 6
D
2, 3, 4, 5, 6, 7, 1
Question 24 Explanation: 
It is nothing but a pairwise swapping of the linked list.
Question 25

The following C function takes two ASCII strings and determines whether one is an anagram of the other. An anagram of a string s is a string obtained by permuting the letters in s.

int anagram (char *a, char *b) {
     int count [128], j;
     for (j = 0;  j < 128; j++) count[j] = 0;
     j = 0;
     while (a[j] && b[j]) {
       A;
       B;
}
for (j = 0; j < 128; j++) if (count [j]) return 0;
return 1;
} 

Choose the correct alternative for statements A and B.

A
A : count [a[j]]++ and B : count[b[j]]–
B
A : count [a[j]]++ and B : count[b[j]]++
C
A : count [a[j++]]++ and B : count[b[j]]–
D
A : count [a[j]]++and B : count[b[j++]]–
Question 25 Explanation: 
A: Increments the count by 1 at each index that is equal to the ASCII value of the alphabet, it is pointing at.
B: Decrements the count by 1 at each index that is equal to the ASCII value of the alphabet it is pointing at. Also it increments the loop counter for next iteration.
If one string is permutation of other, there would have been equal increments and decrements at each index of array, and so count should contain zero at each index, that is what the loop checks at last and if any non-zero elements is found, it returns 0 indicating that strings are not anagram to each other.
Question 26

The following function computes the value of mCn correctly for all legal values m and n (m≥1,n≥0 and m>n)

 int func(int m, int n)
{
    if (E) return 1;
    else return(func(m - 1, n) + func(m - 1, n - 1));
}

In the above function, which of the following is the correct expression for E?

A
(n == 0) || (m == 1)
B
(n == 0) && (m == 1)
C
(n == 0) || (m == n)
D
(n == 0) && (m == n)
Question 26 Explanation: 
We know that,
mC0 = 1
nCn = 1
Question 27
Which one of the choices given below would be printed when the following program is executed?
#include 
void swap (int *x, int *y)
{
    static int *temp;
    temp = x;
    x = y;
    y = temp;
}
void printab ()
{
    static int i, a = -3, b = -6;
    i = 0;
    while (i <= 4)
    {
        if ((i++)%2 == 1) continue;
        a = a + i;
        b = b + i;
    }
    swap (&a, &b);
    printf("a =  %d, b = %d\n", a, b);
}
main()
{
    printab();
    printab();
}
A
a = 0, b = 3
a = 0, b = 3
B
a = 3, b = 0
a = 12, b = 9
C
a = 3, b = 6
a = 3, b = 6
D
a = 6, b = 3
a = 15, b = 12
Question 27 Explanation: 
First of all, the swap function just swaps the pointers inside the function and has no effect on the variable being passed.
Inside print 'a' and 'b' are added to odd integers from 1 to 5, i.e., 1+3+5=9. So, in first call to print ab,
a = -3+9 = 6
b = -6+9 = 3
Static variable have one memory throughout the program run (initialized during program start) and they keep their values across function calls. So during second call to print ab,
a = 6+9 = 15
b = 3+9 = 12
There are 27 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access