250+ TOP MCQs on Static Variables and Answers

C questions and puzzles on “Static Variables” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         m();
  5.         m();
  6.     }
  7.     void m()
  8.     {
  9.         static int x = 5;
  10.         x++;
  11.         printf("%d", x);
  12.     }

a) 6 7
b) 6 6
c) 5 5
d) 5 6

Answer: a
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         static int x;
  5.         printf("x is %d", x);
  6.     }

a) 0
b) 1
c) Junk value
d) Run time error

Answer: a
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     static int x;
  3.     void main()
  4.     {
  5.         int x;
  6.         printf("x is %d", x);
  7.     }

a) 0
b) Junkvalue
c) Run time error
d) Nothing

Answer: b
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         static double x;
  5.         int x;
  6.         printf("x is %d", x);
  7.     }

a) Nothing
b) 0
c) Compile time error
d) Junkvalue

Answer: c
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         static int x;
  5.         if (x++ < 2)
  6.         main();
  7.     }

a) Infinite calls to main
b) Run time error
c) Varies
d) main is called twice

Answer: d
Clarification: None.

6. Which of following is not accepted in C?
a) static a = 10; //static as
b) static int func (int); //parameter as static
c) static static int a; //a static variable prefixed with static
d) all of the mentioned

Answer: c
Clarification: None.

7. Which of the following cannot be static in C?
a) Variables
b) Functions
c) Structures
d) None of the mentioned

Answer: d
Clarification: None.

250+ TOP MCQs on Pointers and Function Arguments and Answers

C MCQs (multiple choice questions) on “Pointers and Function Arguments”. One shall practice these MCQs to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our multiple choice questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C multiple choice questions on “Pointers and Function Arguments” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     void foo(int*);
  3.     int main()
  4.     {
  5.         int i = 10;
  6.         foo((&i)++);
  7.     }
  8.     void foo(int *p)
  9.     {
  10.         printf("%dn", *p);
  11.     }

a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault/code crash
Answer: c
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     void foo(int*);
  3.     int main()
  4.     {
  5.         int i = 10, *p = &i;
  6.         foo(p++);
  7.     }
  8.     void foo(int *p)
  9.     {
  10.         printf("%dn", *p);
  11.     }

a) 10
b) Some garbage value
c) Compile time error
d) Segmentation fault
Answer: a
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     void foo(float *);
  3.     int main()
  4.     {
  5.         int i = 10, *p = &i;
  6.         foo(&i);
  7.     }
  8.     void foo(float *p)
  9.     {
  10.         printf("%fn", *p);
  11.     }

a) 10.000000
b) 0.000000
c) Compile time error
d) Undefined behaviour
Answer: b
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 97, *p = &i;
  5.         foo(&i);
  6.         printf("%d ", *p);
  7.     }
  8.     void foo(int *p)
  9.     {
  10.         int j = 2;
  11.         p = &j;
  12.         printf("%d ", *p);
  13.     }

a) 2 97
b) 2 2
c) Compile time error
d) Segmentation fault/code crash
Answer: a
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 97, *p = &i;
  5.         foo(&p);
  6.         printf("%d ", *p);
  7.         return 0;
  8.     }
  9.     void foo(int **p)
  10.     {
  11.         int j = 2;
  12.         *p = &j;
  13.         printf("%d ", **p);
  14.     }

a) 2 2
b) 2 97
c) Undefined behaviour
d) Segmentation fault/code crash
Answer: a
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 11;
  5.         int *p = &i;
  6.         foo(&p);
  7.         printf("%d ", *p);
  8.     }
  9.     void foo(int *const *p)
  10.     {
  11.         int j = 10;
  12.         *p = &j;
  13.         printf("%d ", **p);
  14.     }

a) Compile time error
b) 10 10
c) Undefined behaviour
d) 10 11
Answer: a
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 10;
  5.         int *p = &i;
  6.         foo(&p);
  7.         printf("%d ", *p);
  8.         printf("%d ", *p);
  9.     }
  10.     void foo(int **const p)
  11.     {
  12.         int j = 11;
  13.         *p = &j;
  14.         printf("%d ", **p);
  15.     }

a) 11 11 11
b) 11 11 Undefined-value
c) Compile time error
d) Segmentation fault/code-crash
Answer: b
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         int i = 10;
  5.         int *const p = &i;
  6.         foo(&p);
  7.         printf("%dn", *p);
  8.     }
  9.     void foo(int **p)
  10.     {
  11.         int j = 11;
  12.         *p = &j;
  13.         printf("%dn", **p);
  14.     }

a) 11 11
b) Undefined behaviour
c) Compile time error
d) Segmentation fault/code-crash
Answer: a
Clarification: None.

9. Which of the following is the correct syntax to send an array as a parameter to function?
a) func(&array);
b) func(#array);
c) func(*array);
d) func(array[size]);
Answer: a
Clarification: None.

250+ TOP MCQs on Command Line Arguments and Answers

C Objective Questions on “Command Line Arguments”. One shall practice these Objective Questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C Objective Questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C Objective Questions on “Command Line Arguments” along with answers, explanations and/or solutions:

1. What does argc and argv indicate in command-line arguments?
(Assuming: int main(int argc, char *argv[]) )
a) argument count, argument variable
b) argument count, argument vector
c) argument control, argument variable
d) argument control, argument vector
Answer: b
Clarification: None.

2. Which of the following syntax is correct for command-line arguments?
a)

int main(int var, char *varg[])

b)

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

c)

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

d) none of the mentioned
Answer: a
Clarification: None.

3. In linux, argv[0] by command-line argument can be occupied by _________
a) ./a.out
b) ./test
c) ./fun.out.out
d) all of the mentioned
Answer: d
Clarification: All the options mentioned (./a.out, ./test, ./fun.out.out) are simply the command without any argument. A command is always stored as argument vector zero i.e., argv[0] always contain the command where as argv[1], argv[2], etc. contains the arguments to the commands, if any.

4. What type of array is generally generated in Command-line argument?
a) Single dimension array
b) 2-Dimensional Square Array
c) Jagged Array
d) 2-Dimensional Rectangular Array
Answer: c
Clarification: None.

5. What will be the output of the following C statement? (assuming the input is “cool brother in city”)

  printf(%sn”, argv[argc]);

a) (null)
b) City
c) In
d) Segmentation Fault
Answer: a
Clarification: None.

6. What is the first argument in command line arguments?
a) The number of command-line arguments the program was invoked with;
b) A pointer to an array of character strings that contain the arguments
c) Nothing
d) None of the mentioned
Answer: a
Clarification: None.

7. What is the second argument in command line arguments?
a) The number of command-line arguments the program was invoked with;
b) A pointer to an array of character strings that contain the arguments, one per string
c) Nothing
d) None of the mentioned
Answer: b
Clarification: None.

8. What is argv[0] in command line arguments?
a) The name by which the program was invoked
b) The name of the files which are passed to the program
c) Count of the arguments in argv[] vector
d) None of the mentioned
Answer: a
Clarification: None.

250+ TOP MCQs on Table Lookup and Answers

C helps anyone preparing for Brocade and other companies C interviews. One should practice these Objective Questions and answers continuously for 2-3 months to clear Brocade interviews on C Programming language.

Here is a listing of online C quiz on “Table Lookup” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     struct student
  3.     {
  4.         char a[5];
  5.     };
  6.     void main()
  7.     {
  8.         struct student s[] = {"hi", "hey"};
  9.         printf("%c", s[0].a[1]);
  10.     }

a) h
b) i
c) e
d) y
Answer: b
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         char *a[3] = {"hello", "this"};
  5.         printf("%s", a[1]);
  6.     }

a) hello
b) Varies
c) this
d) Compile time error
Answer: c
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         int lookup[100] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  5.         printf("%d", lookup[3]);
  6.     }

a) 2
b) 4
c) Compile time error
d) 3
Answer: d
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     void main()
  3.     {
  4.         char *a[3][3] = {{"hey", "hi", "hello"}, {"his", "her", "hell"}
  5.         , {"hellos", "hi's", "hmm"}};
  6.         printf("%s", a[1][1]);
  7.     }

a) her
b) hi
c) Compile time error
d) hi’s
Answer: a
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     struct p
  3.     {
  4.         char *name;
  5.         struct p *next;
  6.     };
  7.     struct p *ptrary[10];
  8.     int main()
  9.     {
  10.         struct p p;
  11.         p->name = "xyz";
  12.         p->next = NULL;
  13.         ptrary[0] = &p;
  14.         printf("%sn", p->name);
  15.         return 0;
  16.     }

a) Compile time error
b) Segmentation fault/code crash
c) xyz
d) Undefined behaviour
Answer: a
Clarification: None.

6. What will be the output of the following C code?

  1.     #include 
  2.     struct p
  3.     {
  4.         char *name;
  5.         struct p *next;
  6.     };
  7.     struct p *ptrary[10];
  8.     int main()
  9.     {
  10.         struct p p;
  11.         p.name = "xyz";
  12.         p.next = NULL;
  13.         ptrary[0] = &p;
  14.         printf("%sn", ptrary[0]->name);
  15.         return 0;
  16.     }

a) Compile time error
b) Segmentation fault
c) Undefined behaviour
d) xyz
Answer: d
Clarification: None.

7. What will be the output of the following C code?

  1.     #include 
  2.     struct p
  3.     {
  4.         char *name;
  5.         struct p *next;
  6.     };
  7.     struct p *ptrary[10];
  8.     int main()
  9.     {
  10.         struct p p, q;
  11.         p.name = "xyz";
  12.         p.next = NULL;
  13.         ptrary[0] = &p;
  14.         strcpy(q.name, p.name);
  15.         ptrary[1] = &q;
  16.         printf("%sn", ptrary[1]->name);
  17.         return 0;
  18.     }

a) Compile time error
b) Segmentation fault/code crash
c) Depends on the compiler
d) xyz
Answer: b
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         struct p
  5.         {
  6.             char *name;
  7.             struct p *next;
  8.         };
  9.         struct p p, q;
  10.         p.name = "xyz";
  11.         p.next = NULL;
  12.         ptrary[0] = &p;
  13.         strcpy(q.name, p.name);
  14.         ptrary[1] = &q;
  15.         printf("%sn", ptrary[1]->name);
  16.         return 0;
  17.     }

a) Compile time error
b) Depends on the compiler
c) Undefined behaviour
d) xyz
Answer: c
Clarification: None.

250+ TOP MCQs on File Access and Answers

C helps anyone preparing for Adaptec and other companies C interviews. One should practice these Objective Questions and answers continuously for 2-3 months to clear Adaptec interviews on C Programming language.

Here is a listing of C programming questions on “File Access” along with answers, explanations and/or solutions:

1. Which of the following fopen() statements are illegal?
a) fp = fopen(“abc.txt”, “r”);
b) fp = fopen(“/home/user1/abc.txt”, “w”);
c) fp = fopen(“abc”, “w”);
d) none of the mentioned
Answer: d
Clarification: None.

2. What does the following segment of C code do?

a) It writes “Copying!” into the file pointed by fp
b) It reads “Copying!” from the file and prints on display
c) It writes as well as reads “Copying!” to and from the file and prints it
d) None of the mentioned
Answer: a
Clarification: None.

3. What is FILE reserved word?
a) A structure tag declared in stdio.h
b) One of the basic data types in c
c) Pointer to the structure defined in stdio.h
d) It is a type name defined in stdio.h
Answer: d
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         FILE *fp = stdin;
  5.         int n;
  6.         fprintf(fp, "%d", 45);
  7.     }

a) Compilation error
b) 45
c) Nothing
d) Depends on the standard
Answer: c
Clarification: None.

5. What will be the output of the following C code?

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         FILE *fp = stdout;
  6.         int n;
  7.         fprintf(fp, "%d", 45);
  8.     }

a) Compilation error
b) 45
c) Nothing
d) Depends on the standard
Answer: b
Clarification: None.

6. stdout, stdin and stderr are ________
a) File pointers
b) File descriptors
c) Streams
d) Structure
Answer: a
Clarification: None.

7. Which of the following statements about stdout and stderr are true?
a) Same
b) Both connected to screen always
c) Both connected to screen by default
d) stdout is line buffered but stderr is unbuffered
Answer: c
Clarification: None.

8. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         FILE *fp = stdout;
  5.         int n;
  6.         fprintf(fp, "%d ", 45);
  7.         fprintf(stderr, "%d ", 65);
  8.         return 0;
  9.     }

a) 45 65
b) 65 45
c) 65
d) Compilation error
Answer: b
Clarification: None.

9. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         FILE *fp = stdout;
  5.         int n;
  6.         fprintf(fp, "%dn ", 45);
  7.         fprintf(stderr, "%d ", 65);
  8.         return 0;
  9.     }

a) 45 65
b) 65 45
c) 65
d) Compilation error
Answer: a
Clarification: None.

10. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         FILE *fp = stdout;
  5.         int n;
  6.         fprintf(fp, "%d ", 45);
  7.         fflush(stdout);
  8.         fprintf(stderr, "%d", 65);
  9.         return 0;
  10.     }

a) 45 65
b) 65 45
c) 45
d) Compilation error
Answer: a
Clarification: None.

250+ TOP MCQs on Random Number Generation and Answers

C Objective Questions on “Random Number Generation”. One shall practice these Objective Questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C Objective Questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C Objective Questions on “Random Number Generation” along with answers, explanations and/or solutions:

1. What will be the output of the following C code?

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         printf("%dn", rand() % 1000);
  6.         return 0;
  7.     }

a) Compile time error
b) An integer between 0-1000
c) An integer between 0-999 including 0 and 999
d) An integer between 0-1000 including 1000
Answer: c
Clarification: None.

2. What will be the output of the following C code?

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         srand(9000);
  6.         printf("%dn", rand());
  7.         return 0;
  8.     }

a) Compile time error
b) An integer in the range 0 to RAND_MAX
c) A double in the range 0 to 1
d) A float in the range 0 to 1
Answer: b
Clarification: None.

3. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         printf("%dn", srand(9000));
  5.         return 0;
  6.     }

a) Compile time error
b) An integer in the range 0 to 9000
c) A float in the range 0 to 1
d) A double in the range 0 to 9000
Answer: a
Clarification: None.

4. What will be the output of the following C code?

  1.     #include 
  2.     int main()
  3.     {
  4.         srand(time(NULL));
  5.         printf("%dn", rand());
  6.         return 0;
  7.     }

a) Compile time error
b) An integer in the range 0 to RAND_MAX
c) A double in the range 0 to 1
d) A float in the range 0 to 1
Answer: b
Clarification: None.

5. In the below C program, every time program is run different numbers are generated.

  1.     #include 
  2.     #include 
  3.     int main()
  4.     {
  5.         printf("%dn", rand());
  6.         return 0;
  7.     }

a) True
b) False
c) Depends on the platform
d) Depends on the compiler
Answer: b
Clarification: None.

6. In the following C program, every time program is run different numbers are generated.

  1.     #include 
  2.     int main()
  3.     {
  4.         srand(time(NULL));
  5.         printf("%dn", rand());
  6.         return 0;
  7.     }

a) True
b) False
c) Depends on the platform
d) Depends on the compiler
Answer: a
Clarification: None.

7. Which of these is a correct way to generate numbers between 0 to 1(inclusive) randomly?
a) rand() / RAND_MAX
b) rand() % 2
c) rand(0, 1)
d) none of the mentioned
Answer: a
Clarification: None.