Arithmetic Operation · Programming in C
6 questions · asked in 6 of 27 years (2000–2026)
MCQ 4 · NAT 21-mark 3 · 2-mark 3
2000Questions per year2026
Questions 1–5 of 6
Consider the following C program:
#include <stdio.h>
int main(){
int a = 6;
int b = 0;
while(a < 10) {
a = a / 12 + 1;
a += b;}
printf("%d", a);
return 0;}
Which one of the following statements is CORRECT?Answer: (C)
The program uses integer division.
First pass: , and after that .
Second pass: once more, and the same repeats on every pass.
The value of stays at 1. So the condition is true every time, and printf is never reached.
Answer: The program gets stuck in an infinite loop
First pass: , and after that .
Second pass: once more, and the same repeats on every pass.
The value of stays at 1. So the condition is true every time, and printf is never reached.
Answer: The program gets stuck in an infinite loop
What is printed by the following ANSI C program?
ASCII encoding for relevant characters is given below
#include <stdio.h>
int main(int argc, char *argv[]){
char a = 'P';
char b = 'x';
char c = (a & b) + '*';
char d = (a | b) - '-';
char e = (a ^ b) + '+';
printf("%c %c %c \n", c, d, e);
return 0;
}
ASCII encoding for relevant characters is given below
Answer: (A)
The ASCII codes are 'P' = 80 = 0101 0000 and 'x' = 120 = 0111 1000.
- AND: = 0101 0000 = 80. Then 80 + '*'(42) = 122, which is 'z'
- OR: = 0111 1000 = 120. Then 120 '-'(45) = 75, which is 'K'
- XOR: = 0010 1000 = 40. Then 40 + '+'(43) = 83, which is 'S'
The printf call uses %c, so it prints characters: z K S
Answer: z K S
- AND: = 0101 0000 = 80. Then 80 + '*'(42) = 122, which is 'z'
- OR: = 0111 1000 = 120. Then 120 '-'(45) = 75, which is 'K'
- XOR: = 0010 1000 = 40. Then 40 + '+'(43) = 83, which is 'S'
The printf call uses %c, so it prints characters: z K S
Answer: z K S
Consider the following C program.
#include <stdio.h>
int main ( ) {
int m = 10;
int n, n1;
n = ++m;
n1 = m++;
n--;
--n1;
n-=n1;
printf ("%d", n) ;
return 0;
} The output of the program is ______________.Answer: 0
Go through each statement in order:
- n = ++m: m = 11, n = 11
- n1 = m++: n1 = 11, m = 12
- Decrement n: n-- 10. Decrement n1: --n1 10
- Subtract n1 from n: n -= n1 0
Answer: 0
- n = ++m: m = 11, n = 11
- n1 = m++: n1 = 11, m = 12
- Decrement n: n-- 10. Decrement n1: --n1 10
- Subtract n1 from n: n -= n1 0
Answer: 0
The attributes of three arithmetic operators in some programming language are given below.
The value of the expression 2-5+1-7*3 in this language is_______ .
The value of the expression 2-5+1-7*3 in this language is_______ .
Answer: 9
Order of precedence: + is highest * is lowest. So * is done last:
Inside the bracket, + goes first, 5 + 1 = 6 2 6 7. is right associative: .
Answer: 9
Inside the bracket, + goes first, 5 + 1 = 6 2 6 7. is right associative: .
Answer: 9
Suppose n and p are unsigned int variables in a C program. We wish to set p to . If n is large, which one of the following statements is most likely to set p correctly?
Answer: (B)
. In C, and are unsigned int, so every step is done in unsigned int, left to right, and integer division drops the fraction. We need a form with an exact division at each step and small intermediate values.
- (A) The product is computed first. For large it overflows before the division by 6.
- (B) is , always a whole number. Multiplying by gives , so the division by 3 is exact. The largest intermediate value is only half of that in (A), so it is right for a larger range of .
- (C) is not always whole. For : , then , and , but .
- (D) The product is still computed in unsigned int first, and overflows for large before the division by turns it into a double.
Answer: (B)
- (A) The product is computed first. For large it overflows before the division by 6.
- (B) is , always a whole number. Multiplying by gives , so the division by 3 is exact. The largest intermediate value is only half of that in (A), so it is right for a larger range of .
- (C) is not always whole. For : , then , and , but .
- (D) The product is still computed in unsigned int first, and overflows for large before the division by turns it into a double.
Answer: (B)
Keep practising