Programming

Question 1

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 1 Explanation: 
If we use "goto" statements then it leads to structural decomposition of code then it is difficult to verify the programs.
Question 2

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 2 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 3

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 3 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 4

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 4 Explanation: 
If it is call by reference then answer is 42.
If it is call by value then answer is 36.
Question 5

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 5 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 6
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 6 Explanation: 
0, because global m is not modified, m is just passed to formal argument of P.
Question 7
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 7 Explanation: 
0, because n is just passed to formal parameters of Q and no modification in global n.
Question 8
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 8 Explanation: 
Since m is defined global it is visible inside all the procedures.
Question 9

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 9 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 10

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 10 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 11

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 11 Explanation: 
It is nothing but a pairwise swapping of the linked list.
Question 12

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 12 Explanation: 

Hence, 9 is the answer.
Question 13

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

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 14 Explanation: 
x = 1 - x
For x = 0, it gives 1.
For x = 1, it gives 0.
Question 15

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 15 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 16

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 16 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 17

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 17 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 18

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 18 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 19

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 19 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 20

Which one of the choices given below would be printed when the following program is executed?

#include 
struct test {
               int i;
               char *c;
}st[] = {5, "become", 4, "better", 6, "jungle", 8, "ancestor", 7, "brother"};
main ()
{ 
    struct test *p = st;
    p += 1;
    ++p -> c;
    printf("%s,", p++ -> c);
    printf("%c,", *++p -> c);
    printf("%d,", p[0].i);
    printf("%s n", p -> c);
}
A
jungle, n, 8, ncestor
B
etter, u, 6, ungle
C
cetter, k, 6, jungle
D
etter, u, 8, ncestor
Question 20 Explanation: 
Lets take the part of program,
Line 1 - main ( )
Line 2 - {
Line 3 - struct test *p = st;
Line 4 - p += 1;
Line 5 - ++p → c;
Line 6 - printf("%s", p++→ c);
Line 7 - printf("%c", +++p → c);
Line 8 - printf("%d", p[0].i);
Line 9 - printf("%s\n", p → c);
Line 10 - }
Now,
Line 3: Initially p is pointing to st, i.e., first element of st which is {5, "become"}
Line 4: Now p is pointing to {4, "better"}
Line 5: ++(p → c), since → has higher precedence, so p → c points to 'e' of "better".
Line 6: prints 'enter' and p now points to {6, "jungle"}
Line 7: ***(p → c), since → has higher precedence. So, prints 'u'.
Line 8: p → i, which is 6 so prints '6'.
Line 9: prints 'ungle' since p is pointing to 'u'.
So, output is "enter, u, 6, ungle".
Question 21
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 21 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
Question 22

Which one of the choices given below would be printed when the following program is executed?

#include 
int a1[] = {6, 7, 8, 18, 34, 67};
int a2[] = {23, 56, 28, 29};
int a3[] = {-12, 27, -31};
int *x[] = {a1, a2, a3};
void print(int *a[])
{
            printf("%d,", a[0][2]);
            printf("%d,", *a[2]);
            printf("%d,", *++a[0]);
            printf("%d,", *(++a)[0]);
            printf("%d/n", a[-1][+1]);
}
main()
{
             print(x);
}
A
8, -12, 7, 23, 8
B
8, 8, 7, 23, 7
C
-12, -12, 27, -31, 23
D
-12, -12, 27, -31, 56
Question 22 Explanation: 
1) a[0][2] = *(*(a+0)+2)
It returns the value of 3rd element in a1.
First printf print 8.
2) *a[2] = *(*(a+2))
It returns the value of 1st element in a3.
Second printf print -12.
3) *++a[0] = *(++(*(a+0)))
a[0] is pointing to 1st element in a1.
++a[0] - after preincrement performed, now a[0] is pointing to 2nd element in a1.
*++a[0] return the value of 2nd element in a1.
Third printf print 7.
4) *(++a)[0]
++a - after preincrement is performed 'a' is pointing to a2.
(++a)[0] is pointing to 1st element in a2.
*(++a)[0] returns the value of 1st element in a2.
Fourth printf print 23.
5) a[-1][+1] = *(*(a-1)+1)
(a-1) is pointing to a1.
*(a-1) is pointing to the 2nd element in a1, because in 3rd printf already a1 was incremented by 1.
*(a-1)+1 is pointing 3rd element in a1.
*(*(a-1)+1) returns the value of 3rd element in a1, i.e., 8.
Question 23

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 23 Explanation: 
We know that,
mC0 = 1
nCn = 1
Question 24

The function f is defined as follows:

int f (int n) {
       if (n <= 1) return 1;
       else if (n % 2  ==  0) return f(n/2);
       else return f(3n - 1);
} 
Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.
(i) The function f terminates for finitely many different values of n ≥ 1.
(ii) The function f terminates for infinitely many different values of n ≥ 1.
(iii) The function f does not terminate for finitely many different values of n ≥ 1.
(iv) The function f does not terminate for infinitely many different values of n ≥ 1.
Which one of the following options is true of the above?

A
(i) and (iii)
B
(i) and (iv)
C
(ii) and (iii)
D
(ii) and (iv)
Question 24 Explanation: 
The function can be terminated for all the values which can have factor of 2{(2-x)2 == 0}, so (i) is false and (ii) is true.
→ Let n=3, then it is terminated in 2nd iteration.
→ Let n=5, then sequence is 5→14→7→20→10 and it will repeat.
→ Any number with factor 5 and 2 leads to infinite recursion.
So, (iv) is True and (iii) is False.
There are 24 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