250+ TOP MCQs on Error Handling and Answers

’s Objective Questions and Answers on C helps Freshers preparing for Job Interviews. Freshers’s should practice these questions continuously for 2-3 months, thereby ensuring a top position in campus interviews or fresher’s hiring programs.

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

1. Which of the following causes an error?
a) Trying to read a file that doesn’t exist
b) Inability to write data in a file
c) Failure to allocate memory with the help of malloc
d) All of the mentioned
Answer: d
Clarification: None.

2. What is the purpose of the C function?

a) They check for input errors
b) They check for output errors
c) They check for all types of errors
d) They check for error in accessing the file
Answer: b
Clarification: None.

3. stderr is similar to?
a) stdin
b) stdout
c) Both stdout and stdin
d) None of the mentioned
Answer: b
Clarification: stderr is not exactly the same as stdout, but similar in the sense that both puts the output or error to the monitor.

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

fprintf(stderr, "error: could not open filen");

a) The diagnostic output is directly displayed in the output
b) The diagnostic output is pipelined to the output file
c) The line which caused error is compiled again
d) The program is immediately aborted
Answer: a
Clarification: None.

5. Which of the following function can be used to terminate the main() function from another function safely?
a) return(expr);
b) exit(expr);
c) abort();
d) both exit(expr); and abort();
Answer: b
Clarification: None.

6. Which of the following causes an error?
a) Trying to read a file that doesn’t exist
b) Inability to write data in a file
c) Failure to allocate memory with the help of malloc
d) All of the mentioned
Answer: d
Clarification: None.

7. What is the purpose of the C function?

a) They check for input errors
b) They check for output errors
c) They check for all types of errors
d) They check for error in accessing the file
Answer: b
Clarification: None.

250+ TOP MCQs on printf and Answers

C Multiple Choice Questions & Answers on “printf – 2”.

1. If by mistake you specify more number of arguments, the excess arguments will ____________
a) be ignored
b) produce compile error
c) produce run-time error
d) produce logical error
Answer: a
Clarification: The excess arguments will simply be ignored.

2. What happens when zero flag is used with left justification?
a) data is padded with zeros
b) zero flag is ignored
c) data is padded with blank spaces
d) will give error
Answer: b
Clarification: Zero flag is not considered when used with left justification because adding zeros after a number changes its value.

3. For floating point numbers, the precision flag specifies the number of decimal places to be printed. When no precision modifier is specified, printf() prints _______
a) six decimal positions
b) five decimal positions
c) four decimal positions
d) three decimal positions
Answer: a
Clarification: Its format can be given as “. m”, where m specifies the number of decimal digits when no precision modifier is specified, printf prints six decimal positions.

4. What will the given code result in printf(“n you are”awesome ” “);?
a) compile error
b) run-time error
c) you are “awesome”
d) you are awesome
Answer: c
Clarification: The above given code uses ”” to display the word within double inverted commas on standard output screen.

5. What will be the output for the given code printf(“n The number is %07d”,1212);
a) The number is 0001212
b) The number is 1212
c) The number is 1212
d) The number is 1212000
Answer: a
Clarification: 0 in the above code is Flags. The number is left-padded with zeros(0) instead of spaces.

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

char t=’N’;
printf(“n %c n %3c n %5c”,t,t,t);

a)              N
          N
    N
b)    N
            N
                 N
c)  N
      N
      N
d)   N  N  N  
Answer: b
Clarification: In the given code each argument is printed on a new line due to control character n. Width mentioned in the above code is 1,3,5 hence the character is printed on a new line after being padded with blank spaces.

7. Select the right explanation to the given code.

printf(%*. *f”, 5,4,5700);

a) the minimum field width has to be 4, the precision is given to be 5, and the value to be displayed is 5700
b) the minimum field width is 5, the precision is 4, and the value to be displayed is 5700
c) compile error
d) run-time error
Answer: b
Clarification: The minimum field width and precision specifiers are usually constants. They can also be provided by arguments to printf(). This is done by using * modifier as shown in the given code.

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

char str[] = "Hello Nancy“;
printf(“n %.7s”, str) ;

a) Hello Nan
b) Hello
c) Hello N
d) Hello Nancy
Answer: c
Clarification: The output for the code must be 7 characters including white spaces.

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

char str[] =”Too Good”;
printf(“n %7s”,str);

a) Too Good
b) Too G
c) Too Go
d) Too
Answer: a
Clarification: The complete string “Too Good” is printed. This is because if data needs more space than specified, then printf overrides the width specified by the user.

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

printf(“n Output: %5d t %x t %#x”, 234,234,234);

a) Output:234EA0xEA
b) Output:00234 EA 0xEA
c) Output:    234 EA 0xEA
d) ERROR
Answer: c
Clarification: The control character t is used to provide gap between the words. %5d – the width of the string is set to 5, characters are printed after being padded with blank spaces.%x, %#x is additional specifiers for octal and hexadecimal values.

250+ TOP MCQs on Error Handling and Answers

C MCQs on “Error Handling”.

1. _______ occurs when a result is too large in magnitude to represent errors as a floating-point value of the required type.
a) underflow
b) significance loss
c) domain
d) overflow
Answer: d
Clarification: An overflow occurs when a result is too large in magnitude to represent errors as a floating-point value of the required type.

2. What occurs when a result has nowhere near the number of significant digits indicated by its type.
a) domain
b) underflow
c) overflow
d) significance loss
Answer: d
Clarification: A significance loss occurs when a result has nowhere near the number of significant digits indicated by its type.

3. What error occurs when a result is undefined for a given argument value?
a) significance loss
b) underflow
c) overflow
d) domain
Answer: d
Clarification: A domain error occurs when a result is undefined for a given argument value.

4.______ is reported on a domain error.
a) EDOM
b) ERANGE
c) Significance loss
d) Underflow
Answer: a
Clarification: EDOM is reported on a domain error.

5. ERANGE is reported on an overflow or an underflow.
a) true
b) false
Answer: a
Clarification: This macro represents a range error, which occurs if an input argument is outside the range, over which the mathematical function is defined and errno is set to ERANGE.

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

errno = 0;
y = sqrt(2);
if(errno == EDOM)
printf("Invalid valuen");
else
printf("Valid valuen");

a) Invalid value
b) Valid value
c) No output
d) Compile error
Answer: b
Clarification: The C library macro EDOM represents a domain error, which occurs if an input argument is outside the domain, over which the mathematical function is defined and errno is set to EDOM.

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

errno = 0;
y = sqrt(-10);
if(errno == EDOM)
printf("Invalid value n");
else
printf("Valid valuen");

a) Invalid value
b) Valid value
c) No output
d) Compile error
Answer: a
Clarification: The C library macro EDOM represents a domain error, which occurs if an input argument is outside the domain, over which the mathematical function is defined and errno is set to EDOM.

8. errno causes trouble in two subtler ways(vague and explicit).
a) true
b) false
Answer: a
Clarification: errno causes trouble in two subtler ways – sometimes its specification is too vague and sometimes it is too explicit.

9. No library function will store a zero in errno.
a) true
b) false
Answer: a
Clarification: Any library function can store nonzero values in errno.

10. __________ tells the compiler that this data is defined somewhere and will be connected with the linker.
a) errno
b) extern
c) variable
d) yvals
Answer: b
Clarification: The C library macro extern int errno is set by system calls and some library functions in the event of an error to indicate if anything went wrong.

250+ TOP MCQs on Date and Time Functions and Answers

C MCQs on “Date and Time Functions – 1”.

1. Which of the following library functions returns the time in UTC (Greenwich mean time) format?
a) localtime()
b) gettime()
c) gmtime()
d) settime()
Answer: c
Clarification: The library function gmtime() returns the time in UTC format. To find the current time, we use the function localtime().

2. What will be the output of the following C code? (By date we mean: date, month and year)

#include
#include
#include
int main()
{
    time_t ct;
    time(&ct);
    printf("%sn",ctime(&ct));
}

a) only current date
b) only current date and current time
c) current date, current time and the day of the week
d) only current time
Answer: c
Clarification: The code shown above will print the current date, current time and the day of the week as output.

3. What will be the output of the following C code if the current system date is 6/22/2017?

#include
#include
#include
int main()
{
    time_t ct;
    time(&ct);
    struct tm *mt=localtime(&ct);
    printf("%dn",mt-> tm_mon+2);
}

a) 8
b) 7
c) 5
d) 6
Answer: b
Clarification: Since the given date is 22nd of June, 2017. Since June is the sixth month of the year, the output will be 5. (0-Jan, 1-Feb, 2-March, 4-April, 5-May, 6-June……..)

4. What will be the output of the following C code if the current system date is 6/22/2017?

#include
#include
#include
typedef struct tm tm;
int main()
{
    time_t ct;
    time(&ct);
    tm *mt=localtime(&ct);
    printf("%dn",mt-> tm_year);
}

a) 17
b) 2017
c) error
d) 117
Answer: d
Clarification: The output of the code shown above is the number of the current year, counted from the year 1900. Hence, since the current year is 2017, the output will be: 2017-1900 = 117.

5. What will be the output of the following C code if the current system date is 6/22/2017?

#include
#include
#include
int main()
{
    time_t ct;
    time(&ct);
    struct tm *mt=localtime(&ct);
    printf("%dn",mt-> tm_date);
}

a) 22
b) 6
c) 22/6
d) error
Answer: d
Clarification: The code shown above results in an error. This is because tm_date is not a specified function under time.h.

6. The purpose of the function ctime() is that ___________
a) it returns a string representing the local time
b) it returns a void string
c) it returns a string representing the time in UTC format
d) it returns a string representing the time stored in a structure
Answer: a
Clarification: The library function ctime() returns a string representation of the local time. The function asctime() returns a string representing the time stored in a structure.

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

#include
int main (void)
{
    float n = time(NULL);
    printf("%.2fn" , n);
}

a) time in seconds from 1 January, 1970
b) time in minutes from 1 January, 1970
c) time in seconds from 1 January, 1980
d) time in minutes from 1 January, 1980
Answer: a
Clarification: The output of the code shown above will be the time in seconds from 1 January, 1970.

8. What will be the output of the following C code if the system date is 6/2/2017(Friday)?

#include
#include
int main()
{
    struct tm *local, *gm;
    time_t t;
    t=time(NULL);
    local=localtime(&t);
    printf("%d",local->tm_wday);
    return 0;
}

a) 6
b) 5
c) error
d) 0
Answer: b
Clarification: Since the given question has clearly specified that the current weekday at the time of execution of the code is Friday, the output of the code shown above is 5. (0-Sunday, 1-Mon, 2-Tue, 3-Wed, 4 – Thurs, 5-fri, 6-Sat)

9. Which of the following functions returns a pointer to a string representing the date and time stored in a structure?
a) ctime()
b) time()
c) asctime()
d) localtime()
Answer: c
Clarification: The function asctime() returns a pointer to a string representing the date and time stored in a structure.

10. What will be the output of the following C code if it is executed on 2nd January, 2017 (system date)?

#include
#include
int main()
{
    struct tm *local, *gm;
    time_t t;
    t=time(NULL);	
    local=localtime(&t);
    printf("%d",local->tm_yday);
    return 0;
}

a) 0
b) 1
c) 2
d) Error
Answer: b
Clarification: The output of the code shown above is 1. In the question, it is given that the current date is 2nd January, 2017. (1st Jan – 0, 2nd Jan – 1, 3rd Jan – 2)

250+ TOP MCQs on Variable Names and Answers

C Objective Questions on “Variable Names”. One shall practice these Objective Questions to improve their C programming skills needed for various interviews (campus interviews, walk-in 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 the detailed explanation of the answers which helps in better understanding of C concepts.

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

1. C99 standard guarantees uniqueness of __________ characters for internal names.
a) 31
b) 63
c) 12
d) 14
Answer: b
Clarification: ISO C99 compiler may consider only first 63 characters for internal names.

2. C99 standard guarantees uniqueness of ___________ characters for external names.
a) 31
b) 6
c) 12
d) 14
Answer: a
Clarification: ISO C99 compiler may consider only first 31 characters for external names.

3. Which of the following is not a valid variable name declaration?
a) int __a3;
b) int __3a;
c) int __A3;
d) None of the mentioned
Answer: d
Clarification: None.

4. Which of the following is not a valid variable name declaration?
a) int _a3;
b) int a_3;
c) int 3_a;
d) int _3a
Answer: c
Clarification: Variable name cannot start with a digit.

5. Why do variable names beginning with the underscore is not encouraged?
a) It is not standardized
b) To avoid conflicts since assemblers and loaders use such names
c) To avoid conflicts since library routines use such names
d) To avoid conflicts with environment variables of an operating system
Answer: c
Clarification: None.

6. All keywords in C are in ____________
a) LowerCase letters
b) UpperCase letters
c) CamelCase letters
d) None of the mentioned
Answer: a
Clarification: None.

7. Variable name resolution (number of significant characters for the uniqueness of variable) depends on ___________
a) Compiler and linker implementations
b) Assemblers and loaders implementations
c) C language
d) None of the mentioned
Answer: a
Clarification: It depends on the standard to which compiler and linkers are adhering.

8. Which of the following is not a valid C variable name?
a) int number;
b) float rate;
c) int variable_count;
d) int $main;
Answer: d
Clarification: Since only underscore and no other special character is allowed in a variable name, it results in an error.

9. Which of the following is true for variable names in C?
a) They can contain alphanumeric characters as well as special characters
b) It is not an error to declare a variable to be one of the keywords(like goto, static)
c) Variable names cannot start with a digit
d) Variable can be of any length
Answer: c
Clarification: According to the syntax for C variable name, it cannot start with a digit.

250+ TOP MCQs on Assignment Operators & Expressions and Answers

C aptitude questions on “Assignment Operators & Expressions”. One shall practice these aptitude questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams, aptitude tests 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 questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C aptitude questions on “Assignment Operators & Expressions” 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.         int x = 0;
  5.         if (x = 0)
  6.             printf("Its zeron");
  7.         else
  8.             printf("Its not zeron");
  9.     }

a) Its not zero
b) Its zero
c) Run time error
d) None
Answer: a
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         int k = 8;
  5.         int x = 0 == 1 && k++;
  6.         printf("%d%dn", x, k);
  7.     }

a) 0 9
b) 0 8
c) 1 8
d) 1 9
Answer: b
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         char a = 'a';
  5.         int x = (a % 10)++;
  6.         printf("%dn", x);
  7.     }

a) 6
b) Junk value
c) Compile time error
d) 7
Answer: c
Clarification: None.

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

  1.     #include 
  2.     void main()
  3.     {
  4.         1 < 2 ? return 1: return 2;
  5.     }

a) returns 1
b) returns 2
c) Varies
d) Compile time error
Answer: d
Clarification: None.

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

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

a) Run time error
b) Aries
c) -5
d) 5
Answer: c
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 2, y = 1;
  5.         x *= x + y;
  6.         printf("%dn", x);
  7.         return 0;
  8.     }

a) 5
b) 6
c) Undefined behaviour
d) Compile time error
Answer: b
Clarification: None.

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

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

a) 2
b) 1
c) 0.5
d) Undefined behaviour
Answer: a
Clarification: None.

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

  1.     #include 
  2.     int main()
  3.     {
  4.         int x = 1, y = 0;
  5.         x &&= y;
  6.         printf("%dn", x);
  7.     }

a) Compile time error
b) 1
c) 0
d) Undefined behaviour
Answer: a
Clarification: None.