C programming online quiz

243850358

Following quiz provides Multiple Choice Questions (MCQs) related to C Programming Framework. You will have to read all the given answers and choose over the correct answer. If you are not sure about the answer then you can check the answer using Show Answer link.

1. What is the output of the below code snippet?

#include

main() 
{ 
   int a = 5, b = 3, c = 4; 
   
   printf("a = %d, b = %d\n", a, b, c);
}

A – a=5, b=3
B – a=5, b=3, c=0
C – a=5, b=3, 0
D – compile error

Show Answer
Answer: A
Explaination: a=5,b=3 , as there are only two format specifiers for printing.

2. What is the output of the following program?

#include

main()
{ 
   struct { int x;} var = {5}, *p = &var;
   
   printf("%d %d %d",var.x,p->x,(*p).x); 
}

A – 5 5 5
B – 5 5 garbage value
C – 5 5 0
D – Compile error

Show Answer
Answer: A
Explaination: 5 5 5, the two possible ways of accessing structure elements using pointer is by using -> (arrow operator) OR *.

3. Identify the incorrect file opening mode from the following

A – r
B – w
C – x
D – a

Show Answer
Answer: C
Explaination: x, there is no such mode called “x”.

4. What is the output of the below code snippet.

#include

main()
{
   printf("%d", -11%2);
}

A – 1
B – -1
C – 5.5
D – -5.5

Show Answer
Answer: B
Explaination: Modulus (%) operator is meant to give reminder for integer division.

5. What is the built in library function to compare two strings?

A – string_cmp()
B – strcmp()
C – equals()
D – str_compare()

Show Answer
Answer: B
Explaination: strcmp() is the built in function from “string.h” to compare two strings. Returns 0 if both are same strings. Returns -1 if first < second string. Returns 1 first > second.

6. In C programming language, a function prototype is a declaration of the function that just specifies the function’s interface (function’s name, argument types and return type) and extracts the body of the function. By defining the function, we get to know what action a particular function is going to perform.

A – True
B – False

Show Answer
Answer: A
Explaination: The function body shall associate a specific task, hence representing the action.

7. Which header file can be used to define input/output function prototypes and macros?

A – math.h
B – memory.h
C – stdio.h
D – dos.h

Show Answer
Answer: C
Explaination: The stdio.h header is used to define variable types, macros, and various functions for performing input and output operation.

8. Which library function can convert an unsigned long to a string?

A – ltoa()
B – ultoa()
C – system()
D – unsigned long can’t be converted to a string

Show Answer
Answer: B
Explaination: ultoa() – Convert an unsigned long integer into a string.

9. What value strcmp() function returns when two strings are the same?

A – 0
B – 2
C – 1
D – Error

Show Answer
Answer: A
Explaination: The C library function strcmp() compares two strings with each other and return the value accordingly.

int strcmp(const char *str1, const char *str2)

Comparison happens between first string (str1) with second string (str2).

By comparing two strings, the values return by the function strcmp() are,

  • If, str1 is less than str2 then Return value < 0
  • If, str2 is less than str1 then Return value > 0
  • If, str1 is equal to str2 then Return value = 0

10. In the given below statement, what does the “arr” indicate?

 char *arr[30];

A – arr is a array of function
B – arr is a array of 30 characters
C – arr is a pointer to an array
D – arr is a array of 30 character pointers

Show Answer
Answer: D
Explaination: square parenthesis signify as array at declaration and type is char*, so array of character pointers.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s