Breaking News

COMPUTER PROGRAMMING MCQs by General knowledge Solutions

COMPUTER PROGRAMMING MCQs  by General knowledge Solutions

                                                                               











(1)        Given the following definitions and declarations:

typedef struct{char name[20];
               double latitude;
               double longitude;
              }
              mountain;


typedef struct{mountain chain[10];} range;


range *cascades;


What are the types of the following expressions?

1)  &(cascades->chain[1])
2)  cascades->chain[1].name[0]



(A)    1) mountain *
2) char *

(B)    1) mountain
2) char *

(C)    1) mountain
2) char

(D)    1) mountain *
2) char

(E)    1) mountain **
2) char





(2)        Consider a language with the two operators J and J is left associative and binary. is right associative and unary.   has precedence over J.
Add parentheses to the following expression to show how it would be evaluated.

x J y J x

(A)    ( (x J ( ( y)))) J x
(B)    (x J (( ( y)) J x))
(C)    ( x) J ( (( y) J x))
(D)    ( x) J ( ( (y J x)))
(E)    (( x) J ( ( y))) J x




(3)        If you see the following in a legal C program:

a = b->c[d(e)];

one thing that you can say for sure is:

(A)    b is a pointer to a double

(B)    e is an integer
(C)    c is a function that returns a struct
(D)    d is a function that returns an integer
(E)    a and b have the same type


(4)        Given the function body:
{
int i;
char c;
for (i=0; i<max || fscanf(quark, “%c”,&c)!=EOF; i++)
     fprintf(particle, “%c”,c);
}
What would be the correct header for this function body?

(A)    FILE *func(int quark, int particle, double max)
(B)    void func(FILE *quark, FILE *particle, int max)


(C)    FILE func(FILE *quark, FILE *particle, int max)
(D)    void func(FILE quark, FILE particle, int max)
(E)    void func(struct quark, struct particle, int max)


(5)        Consider the following program:



#include <stdio.h>
#include <stdlib.h>

int func(int i);


int main(void)
{
int x[4];
int i;



for (i=0; i<4; i++)
{
     x[i] = func(i);
     printf(“%i ”,x[i]);
}
     return EXIT_SUCCESS;
}

int func(int i)
{
     if (i>0)


          return i + func(i-1);
     else
          return 0;
}

(6) What is the output once the above program is executed?

(A)    0 1 3 6
(B)    0 1 2 3
(C)    0 0 0 0
(D)    1 1 1 1
(E)    3 3 3 3


(7)        In the context of computer science, a library is

(A)    another name for a function prototype
(B)    a memory area where functions variables are stored while the program executes
(C)    a set of compiled functions that can be called by the programmer without having to write them all over again.
(D)    a list of C books that can be used as reference
(E)    another name for hard drive of a computer


(8) Consider the following code fragment:
void onoff(int *pixel)
{
    /* change *pixel to 1 if *pixel is 0 */
    /* change *pixel to 0 if *pixel is 1 */
    /* missing code */
}

(9)        What should be written instead of the /* missing code */ line for the function to behave as expected?

(A)return 1;
(B)return 0;
(C)if (*pixel==1) return 0; else return 1;
(D)*pixel = (*pixel+1)/2;
(E)*pixel = (*pixel+1)%2;


(10)        What is wrong with the following recursive function?

int nope(int i)         /* line A */
{
    if (i<=0)
       return 1;        /* line B */
    else if (i%2 == 0)  /* line C */
       return i;
    else
       nope(i-3);       /* line D */
}

(A)When executed it may get stuck in an infinite loop
(B)Not all paths of the function algorithm return a value
(C)A recursive function must have a void type
(D)The condition i%2==0 is always true
(E)The function name nope might be confused with a logical operator by the compiler


(11)        How would you fix the function code of question 33?

(A)replace line A by void nope(int i)
(B)replace line A by int nono(int i)
(C)replace line B by return EXIT_SUCCESS;
(D)replace line C by else if (i==0)
(E)replace line D by return nope(i-3);

(12)        C is called C because

(A)it is not a very good language and as such deserves a C grade
(B)it started as the B language (B for Bell laboratories) and later evolved into the C language (big hint: This could be the right answer)
(C)the programmer who wrote it was fond of whistling the C note
(D)all the other letters of the alphabet were already used for names of computer languages
(E)in its early developments, the programs written in C often crashed.  The language was nicknamed C as a short for Crash.




Using a recursive algorithm, write a function count_s that counts the number of occurrences of the character ‘s’ in a string

The prototype of the function is
int count_s(char str[], int len);

where str is the string and len is the length of the string (not counting ‘\0’)

Thus count_s(“I miss Mississipi”,17) is 6


int count_s(char str[],int len)
{
     /* complete the following code */
     /* your algorithm must be recursive */
    

     if (len==1)
     {
          if (*str == ‘s’)
             /* to be completed */
             return 1; 
          else
             /* to be completed */
             return 0; 


     }
     else
     {
          if (*str == ‘s’)
             /* to be completed */
             return 1 + count_s(str+1,len-1);  
          else
             /* to be completed */
   return count_s(str+1,len-1);


     }
}



(2)  [5 points]
The elements of an array a of integers are ordered in increasing order.  The array a has a dimension given by the integer size.
Write a function that takes as parameters a, size, and an integer val.  The function returns an integer which is the position of the element of a less or equal to val and closest to val.  If there is no such element, the function returns –1.

The prototype for the function is
int where(int a[],int size, int val);

If a is {1,3,5,7,10} and val is 6, the function returns 2 which is the position of the element equal to 5.
And if a is {1,3,5,7,10} and val is 0, the function returns –1.


int where(int a[],int size, int val)
{
     /* your code goes here */
     /* do not forget any declaration */
     /* write legibly */
     int index,i;
     index = size –1;

     for(i=size-1;i>=0;i--)
          if (a[i]>val) index = i-1;
     return index;


}



No comments