Tuesday, February 11, 2020

1.4. Arrays in C

1. What is right way to Initialize array?
A. int num[6] = { 2, 4, 12, 5, 45, 5 };
B. int n{} = { 2, 4, 12, 5, 45, 5 };
C. int n{6} = { 2, 4, 12 };
D. int n(6) = { 2, 4, 12, 5, 45, 5 };
ANSWER: A

2. What will be the output of the program ?
#include<stdio.h>
void main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
}
A. 3, 2, 15
B. 2, 3, 20
C. 2, 1, 15
D. 1, 2, 5
ANSWER: A

3. if S is an array of 80 characters, then the value assigned to S through the statement scanf("%s",S) with input 1 2 3 4 5 would be
A. "12345"
B. nothing since 12345 is an integer
C. S is an illegal name for string
D. %s cannot be used for reading in values of S
ANSWER: A
EXPLANATION: the input is stored as a string hence of the form "12345"

4. Minimun number of comparison required to compute the largest and second largest element in array is
A. n-[log2n]-2
B. n+[log2n-2]
C. log2n
D. None of these
ANSWER: B

5. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
A. The element will be set to 0.
B. The compiler would report an error.
C. The program may crash if some important data gets overwritten.
D. The array size would appropriately grow.
ANSWER: C
EXPLANATION:If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer. But the modern compilers will take care of this kind of errors.

6. What will be the output of the program ?
#include<stdio.h>
int main()
{
    static int a[2][2] = {1, 2, 3, 4};
    int i, j;
    static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i),
                                    *(*(i+p)+j), *(*(p+j)+i));
        }
    }
    return 0;
}
A. 1, 1, 1, 1
   2, 3, 2, 3
   3, 2, 3, 2
   4, 4, 4, 4
B. 1, 2, 1, 2
   2, 3, 2, 3
   3, 4, 3, 4
   4, 2, 4, 2
C. 1, 1, 1, 1
   2, 2, 2, 2
   2, 2, 2, 2
   3, 3, 3, 3
D. 1, 2, 3, 4
   2, 3, 4, 1
   3, 4, 1, 2
   4, 1, 2, 3
ANSWER:  C
7.Let x be an array. Which of the following operations are illegal?
I.   ++x
II. x+1
III. x++
IV. x*2

A. I and II
B. I, II and III
C. II and III
D. I, III and IV
E. III and IV
ANSWER: D
EXPLANATION: int x[10]; * x will store the base address of array. *
Statement I, III and IV is invalid.
Statement I and III : ++x and x++ are throwing en error while compile (lvalue required as increment operand )
Since, x is storing in the address of the array which is static value which cannot be change by the operand.
Statement IV : x*2 is also throw an error while compile (invalid operands to binary * (have 'int *' and 'int') )
Statement II : x+1 is throw a warning: assignment makes integer from pointer without a cast [enabled by default]

8. What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    char arr[7]="Network";
    printf("%s",arr);
}
Choose all that apply:
A) Network           
B) N       
C) Garbage value  
D) Compilation error            
E) None of the above
ANSWER: C
EXPLANATION: Size of a character array should one greater than total number of characters in any string which it stores. In c every string has one terminating null character. This represents end of the string.

9. What will be output when you will execute following c code?
#include<stdio.h>
enum power{
    Dalai,
    Vladimir=3,
    Barack,
    Hillary
};
void main(){
    float leader[Dalai+Hillary]={1.f,2.f,3.f,4.f,5.f};
    enum power p=Barack;
    printf("%0.f",leader[p>>1+1]);
}

Choose all that apply:
A) 1        
B) 2        
C) 3        
D) Compilation error            
E) None of the above
ANSWER: B
EXPLANATION: Size of an array can be enum constantan.
Value of enum constant Barack will equal to Vladimir + 1 = 3 +1 = 4
So, value of enum variable p  = 4
leader[p >> 1 +1]
= leader[4 >> 1+1]
=leader[4 >> 2]   //+ operator enjoy higher precedence than >> operator.
=leader[1]  //4>>2 = (4 / (2^2) = 4/4 = 1
=2

10. What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    char data[2][3][2]={0,1,2,3,4,5,6,7,8,9,10,11};
    printf("%o",data[0][2][1]);
}

Choose all that apply:
A) 5        
B) 6        
C) 7        
D) Compilation error            
E) None of the above
ANSWER: 5
EXPLANATION: %o in printf statement is used to print number in the octal format.

11. What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    short num[3][2]={3,6,9,12,15,18};
    printf("%d  %d",*(num+1)[1],**(num+2));
}

Choose all that apply:
A) 12 18 
B) 18 18  
C) 15 15  
D) 12 15 
E) Compilation error             
ANSWER: C
EXPLANATION: *(num+1)[1]
=*(*((num+1)+1))
=*(*(num+2))
=*(num[2])
=num[2][0]
=15
And
**(num+2)
=*(num[2]+0)
=num[2][0]
=15

12. What will be the output of the program ?

#include<stdio.h>

int main()
{
    char str[25] = "Codequiz";
    printf("%s\n", str+2);
    return 0;
}
A. Garbage value
B. Error
C. No output
D. dequiz
ANSWER: D
EXPLANATION: char str[25] = “Codequiz”; The variable str is declared as an array of characteres and initialized with a string “Codequiz”.printf(“%s\n”, str+2);
=> In the printf statement %s is string format specifier tells the compiler to print the string in the memory of str+2
=> str is a base address of string “Codequiz”. Therefore str+2 is another memory location i.e baseaddress+2*sizeof(char);
Hence it prints the dequiz.

13. What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    long  myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
    printf("%ld\t",myarr[1][2]);
    printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
    printf("%ld%ld%ld\t" ,*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);  
}

Choose all that apply:

A) 5 66 776          
B) 6 77 667           
C) 6 66 776           
D) Compilation error            
E) None of the above           
ANSWER: B

14. What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int array[2][3]={5,10,15,20,25,30};
    int (*ptr)[2][3]=&array;
    printf("%d\t",***ptr);
    printf("%d\t",***(ptr+1));
    printf("%d\t",**(*ptr+1));
    printf("%d\t",*(*(*ptr+1)+2));
}

Choose all that apply:
A) 5 Garbage 20 30             
B) 10 15 30 20       
C) 5 15 20 30        
D) Compilation error            
E) None of the above
ANSWER: A
EXPLANATION: ptr is pointer to two dimension array.
***ptr
=***&array  //ptr = &array
=**array //* and & always cancel to each other
=*arr[0]  // *array = *(array +0) = array[0]
=array[0][0]
= 5

15.For the following statements will arr[3] and ptr[3] fetch the same character?
char arr[] = "IndiaBIX";
char *ptr = "IndiaBIX";
A. Yes
B. No
ANSWER: A

16. Which of the following statements are correct about the program below?
#include<stdio.h>

int main()
{
    char str[20], *s;
    printf("Enter a string\n");
    scanf("%s", str);
    s=str;
    while(*s != '\0')
    {
        if(*s >= 97 && *s <= 122)
            *s = *s-32;
        s++;
    }
    printf("%s",str);
    return 0;
}
A. The code converts a string in to an integer
B. The code converts lower case character to upper case
C. The code converts upper case character to lower case
D. Error in code
ANSWER: B
EXPLANATION: Ascii value is altered to convert lower case to upper case

17. Which of the following statements are correct about the below declarations?
char *p = "Sanjay";
char a[] = "Sanjay";
                1:There is no difference in the declarations and both serve the same purpose.
                2:p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing to a non-const pointer.
                3:The pointer p can be modified to point to another string, whereas the individual characters within array a can be changed.
                4:In both cases the '\0' will be added at the end of the string "Sanjay".
A. 1, 2
B. 2, 3, 4
C. 3, 4
D. 2, 3
ANSWER: B

18. Which of the following statements mentioning the name of the array begins DOES NOT yield the base address?
                1:When array name is used with the sizeof operator.
                2:When array name is operand of the & operator.
                3:When array name is passed to scanf() function.
                4:When array name is passed to printf() function.
A. A
B. A, B
C. B
D. B, D
ANSWER: B
EXPLANATION: The statement 1 and 2 does not yield the base address of the array. While the scanf() and printf() yields the base address of the array.

19. String operation such as strcat(s, t), strcmp(s, t), strcpy(s, t) and strlen(s) heavily     rely upon.
A) Presence of NULL character
B) Presence of new-line character
C) Presence of any escape sequence
D) None of the mentioned
ANSWER: A

20. Which of the following function compares 2 strings with case-insensitively?
A) strcmp(s, t)
B) strcmpcase(s, t)
C) strcasecmp(s, t)
D) strchr(s, t)
ANSWER: C

21. What will be the value of var for the following?
    var = strcmp(“Hello”, “World”);
A) -1
B) 0
C) 1
D) strcmp has void return-type
ANSWER: A

22. What is the output of this C code (considering sizeof char is 1 and pointer is 4)?

    #include <stdio.h>
    int main()
    {
        char *a[2] = {"hello", "hi"};
        printf("%d", sizeof(a));
        return 0;
    }
A) 9
B) 4
C) 8
D) 10
ANSWER: C
EXPLANATION: There are 2 elements of char and each element has a pointer size of 4.

23. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        char a[2][6] = {"hello", "hi"};
        printf("%d", sizeof(a));
        return 0;
    }
A) 9
B) 12
C) 8
D) 10
ANSWER: B

24. The memory address of the first element of an array is called
A. floor address
B. foundation address
C. first address
D. base address
ANSWER: D

25. Which of the following data structures are indexed structures?

A. linear arrays
B. linked lists
C. both of above
D. none of above
ANSWER: A

26. What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int a=5,b=10,c=15;
    int *arr[3]={&a,&b,&c};
    printf("%d",*arr[*arr[1]-8]);
}

Choose all that apply:
A) 5        
B) 10      
C) 15      
D) Compilation error            
E) None of the above           
ANSWER: D
EXPLANATION: Member of an array cannot be address of auto variable because array gets memory at load time while auto variable gets memory at run time.

27. What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int arr[][3]={{1,2},{3,4,5},{5}};
    printf("%d %d %d",sizeof(arr),arr[0][2],arr[1][2]);
}

Choose all that apply:
A) 12 3 5
B) 18 0 5
C) 12 0 5
D) 18 3 5
E) Compilation error
ANSWER: B
EXPLANATION: If we will not write size of first member of any array at the time of declaration then size of the first dimension is max elements in the initialization of array of that dimension.
So, size of first dimension in above question is 3.
So size of array = (size of int) * (total number of elements) = 2 *(3*3) = 18
Default initial value of rest elements are zero.  So above array will look like:
{
{1,2,0}
{3,4,5},
{5,0,0}
}         

28. What will be the output of the program ?

#include<stdio.h>

int main()
{
    printf("Code", "Quiz\n");
    return 0;
}
A. Error
B. Code Quiz
C. Code
D. Quiz
ANSWER: C
EXPLANATION: printf(“Code”, “Quiz\n”); It prints “Code”. Because ,(comma) operator has Left to Right associativity. After printing “Code”, the statement got terminated.

29. Two dimensional arrays are also called

A. tables arrays
B. matrix arrays
C. both of above
D. none of above
ANSWER: C

30. The memory address of fifth element of an array can be calculated by the formula

A. LOC(Array[5]=Base(Array)+w(5-lower bound), where w is the number of words per memory cell for the array
B. LOC(Array[5])=Base(Array[5])+(5-lower bound), where w is the number of words per memory cell for the array
C. LOC(Array[5])=Base(Array[4])+(5-Upper bound), where w is the number of words per memory cell for the array
D. None of above
ANSWER: A

31. What will be the output of the program ?

#include<stdio.h>

int main()
{
    printf(5+"Code Quiz\n");
    return 0;
}
A. CodeQuiz
B. Code
C. C
D. Quiz
ANSWER: D
EXPLANATION: printf(5+”Code Quiz\n”); It skips the 5 characters and prints the remaining part of the string.Hence the output is “Quiz”

32. An array elements are always stored in ________ memory locations.

A. Sequential
B. Random
C. Sequential and Random
D. None of the above
ANSWER: A

33. In C Programming, If we need to store word “INDIA” then syntax is as below –
char name[];
name = “INDIA”

A. char name[6] = {'I','N','D','I','A'};
B. char name[6] = {'I','N','D','I','A','\0'}
C. char name[6] = {"I","N","D","I","A"}
D. name = "INDIA"
ANSWER: B
EXPLANATION: Extra spaces must be assigned to null if left unused

34. What will be the output of the program ?
#include"stdio.h"
int main()
{
    int arr[5] = {10};
    printf("%d", 0[arr]);
               
    return 0;
}
A. 1
B. 0
C. 10
D. 6
E. None of these
ANSWER: C
EXPLANATION: 0[arr] implies arr[0] which is the first element of the arrary arr

35. What will be the output of the program ?

#include”stdio.h”
void main()
{
float arr[] = {12.4, 2.3, 4.5, 6.7};
printf(“%d”, sizeof(arr)/sizeof(arr[0]));
}

A. 5
B. 4
C. 6
D. 7
ANSWER: B
EXPLANATION: size of arr[0] is 1 and size of arr is 4

36. Which of the following statements are correct ?
                1:A string is a collection of characters terminated by '\0'.
                2:The format specifier %s is used to print a string.
                3:The length of the string can be obtained by strlen().
                4:The pointer CANNOT work on string.
A. 1, 2
B. 1, 2, 3
C. 2, 4
D. 3, 4
ANSWER: B

37. What will be output when you will execute following c code?

#include<stdio.h>
void main(){
    int xxx[10]={5};
    printf("%d %d",xxx[1],xxx[9]);
}

Choose all that apply:
A) Garbage Garbage            
B) 0 0     
C) null null              
D) Compilation error            
E) None of the above           
ANSWER: B
EXPLANATION: If we initialize any array at the time of declaration the compiler will treat such array as static variable and its default value of uninitialized member is zero.

38. What will be output if you will execute following c code?
#include<stdio.h>
#define var 3
void main(){
    char *ptr="cquestionbank";
    printf("%d",-3[ptr]);
}
A) 100    
B) -100  
C) 101     
D) -101   
E) Compilation error             
ANSWER: D

39. What will be output if you will execute following c code?
#include<stdio.h>
void main(){
    long  myarr[2][4]={0l,1l,2l,3l,4l,5l,6l,7l};
    printf("%ld\t",myarr[1][2]);
    printf("%ld%ld\t",*(myarr[1]+3),3[myarr[1]]);
    printf("%ld%ld%ld\t" ,*(*(myarr+1)+2),*(1[myarr]+2),3[1[myarr]]);
  
}
A) 6   66   777      
B) 6   77   667      
C) 5   66   777       
D) 7   77   666      
E) 6   67   667        
ANSWER: B

40. Advantage of a multi-dimension array over pointer array.
A) Pre-defined size.
B) Input can be taken from user.
C) Faster Access.
D) All of the mentioned
ANSWER: D

41. Comment on the following two operations?
    int *a[] = {{1, 2, 3}, {1, 2, 3, 4}}; //- 1
    int b[][] = {{1, 2, 3}, {1, 2, 3, 4}}; //- 2
a) 1 works, 2 doesn’t
b) 2 works, 1 doesn’t
c) Both of them work
d) Neither of them work
ANSWER: D
EXPLANATION: 1 is a two dimensional array but is not assigned to a two dimensional array variable. 2 is a 2d array but the sizes aren't equal

42. What is the output of this C code?

    #include <stdio.h>
    void main()
    {
        char a[10][5] = {"hi", "hello", "fellows"};
        printf("%s", a[2]);
    }
A) fellows
B) fellow
C) fello
D) fell
ANSWER: C

43. Which of the following statements are true?
    P. Pointer to Array
    Q. Multi-dimensional array
A) P are static, Q are static
B) P are static, Q are dynamic
C) P are dynamic, Q are static
D) P are dynamic, Q are dynamic
ANSWER: C

44. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        char *p[1] = {"hello"};
        printf("%s", (p)[0]);
        return 0;
    }
A) Compile time error
B) Undefined behaviour
C) hello
D) None of the mentioned
ANSWER: C

45. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int i = 0, j = 1;
        int *a[] = {&i, &j};
        printf("%d", (*a)[0]);
        return 0;
    }
A) Compile time error
B) Undefined behaviour
C) 0
D) Some garbage value
ANSWER: C

46. What is the correct syntax to send a 3-dimensional array as a parameter?
    (Assuming declaration int a[5][4][3];)
a) func(a);
b) func(&a);
c) func(*a);
d) func(**a);
ANSWER: A
EXPLANATION: The array is passed as a single variable parameter even though its a multidimensional array.

47. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int ary[2][3];
        foo(ary);
    }
    void foo(int *ary[])
    {
        int i = 10, j = 2, k;
        ary[0] = &i;
        ary[1] = &j;
        *ary[0] = 2;
        for (k = 0;k < 2; k++)
        printf("%d\n", *ary[k]);
    }
A) 2 2
B) Compile time error
C) Undefined behaviour
D) 10 2
ANSWER: A

48. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int ary[2][3][4], j = 20;
        ary[0][0] = &j;
        printf("%d\n", *ary[0][0]);
    }
A) Compile time error
B) 20
C) Address of j
D) Undefined behaviour
ANSWER: A

49. What is the output of this C code?

    #include <stdio.h>
    int main()
    {
        int ary[2][3];
        ary[][] = {{1, 2, 3}, {4, 5, 6}};
        printf("%d\n", ary[1][0]);
    }
A) Compile time error
B) 4
C) 1
D) 2
ANSWER: A

50. What is the output of this C code?

    #include <stdio.h>
    void main()
    {
        char *a[10] = {"hi", "hello", "how"};
        int i = 0;
        for (i = 0;i < 10; i++)
        printf("%s", a[i]);
    }
A) hi hello how Segmentation fault
B) hi hello how null
C) hey hello how Segmentation fault
D) hi hello how followed by 7 nulls
ANSWER: D


Previous                Next

No comments:

Post a Comment