Question 1Medium
- JKJ
- HJH
- IJI
- JH
Answer: A. JKJ
Explanation
This series consists of a simple alphabetical order with the first two letters of all segments: B, C, D, E, F, G, H, I, J, K.
The third letter of each segment is a repetition of the first letter.
Step-by-step solution
- Each term has three letters: first letter, next letter, then the first letter again.
- The first letters go B, D, F, H, which step forward by 2, so the next first letter is J.
- The middle letters go C, E, G, I, which step forward by 2, so the next middle letter is K.
- So the next term is J, K, J.
- The answer is JKJ.
Why the other options are wrong
- B. HJH repeats H as the first letter, but the first letters go B, D, F, H, J.
- C. IJI uses I as the first letter, but the pattern gives J.
- D. JH has only two letters, but each term has three.
Tip: Check the step between the first letters and the step between the middle letters separately.
Question 2Medium
Which data type is most suitable for storing a number 65000 in a 32-bit system?
short
int
long
double
Answer: B.
Explanation
short holds only up to 32767 so 65000 does not fit; int is 32 bits and fits
Step-by-step solution
- 65000 is larger than 32767, the largest value a short holds.
- So short cannot store it.
- An int on a 32-bit system holds values up to about 2147483647, so it fits.
- Option B, int, is the correct answer.
Why the other options are wrong
- A. short holds only up to 32767, so 65000 does not fit.
- C. long can hold 65000, but int is the natural size for this system and the correct answer.
- D. double can store the number but is a floating-point type, not the right type for a whole number.
Tip: Short is 16 bits and holds about -32768 to 32767; int is 32 bits on common 32-bit systems.
Question 3Medium
In following question, a number series is given with one term missing. Choose the correct alternative that will same pattern and fill in the blank spaces.
4, 10, , 82, 244, 730
- 24
- 218
- 77
- 28
Answer: D. 28
Explanation
Each number in the series is the preceding number multiplied by 3
and then decreased by 2.
4 x 3 - 2 = 10
10 x 3 -2 = 28 and so on
Step-by-step solution
- 4 = 3^1 + 1, and 10 = 3^2 + 1.
- The next terms follow the same rule:
82 = 3^4 + 1,
244 = 3^5 + 1 and
730 = 3^6 + 1.
- So the missing term is 3^3 + 1 = 27 + 1.
- So the answer is 28.
Why the other options are wrong
- A. 24 does not fit the pattern, because 24 - 1 = 23 is not a power of 3.
- B. 218 does not fit the pattern, because 218 - 1 = 217 is not a power of 3.
- C. 77 does not fit the pattern, because 77 - 1 = 76 is not a power of 3.
Tip: Test the pattern on the known terms before choosing an answer.
Question 4Medium
What will be output when you will execute following c code?
#include <stdio.h>
int main()
{
signed x;
unsigned y;
x = 10 +- 10u + 10u +- 10;
y = x;
if(x==y)
printf("%d %d",x,y);
else if(x!=y)
printf("%u %u",x,y);
return 0;
}
- 0 0
- 65536 -10
- 0 65536
- Compilation error
Answer: A. 0 0
Explanation
Turbo C++ 3.0: 0 0
Turbo C ++4.5: 0 0
Linux GCC: 0 0
Visual C++: 0 0
Consider on the expression:
x = 10 +- 10u + 10u +- 10;
10: It is signed integer constant.
10u: It is unsigned integer constant.
X: It is signed integer variable.
In any binary operation of dissimilar data type for example: a + b.Lower data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.As we know operators enjoy higher precedence than binary operators. So our expression is:
x = 10 + (-10u) + 10u + (-10);
= 10 + -10 + 10 + (-10);
= 0
Note: Signed is higher data type than unsigned int.So, Corresponding signed value of unsigned 10u is +10
Step-by-step solution
- The expression is 10 + (-10u) + 10u + (-10), where the unsigned constants make the sum wrap around modulo 2 to the power 32.
- -10u equals 4294967286, and 10 + 4294967286 equals 4294967296, which wraps to 0.
- Adding 10u gives 10, and adding -10 (converted to unsigned) wraps back to 0, so
x is 0. y = x gives y = 0, so x == y is true and the first printf runs.- The output is 0 0.
Why the other options are wrong
- B. The sum is 0 and the else branch never runs, so 65536 and -10 are not printed.
- C. Both x and y are 0, so the printed output is 0 0, not 0 65536.
- D. The code is valid C, so there is no compilation error.
Tip: Mixing signed and unsigned values in arithmetic converts the signed one to unsigned.
Question 5Medium
What is the output of this C code?
#include<stdio.h>
int main()
{
float x = 'a';
printf("%f", x);
return 0;
}
- a
- Run time error
- a.0000000
- 97.000000
Answer: D. 97.000000
Explanation
The character 'a' has the code 97. Assigning it to a float gives 97.0, and %f prints 97.000000.
97.000000
Step-by-step solution
- The code assigns the ASCII value of 'a' (which is 97) to the float variable x.
- ASCII values are integers, but they can be assigned to floating-point variables in C.
- When printed as a float, the integer 97 is displayed as 97.000000.
Why the other options are wrong
- A. The code does not output 'a' as a character.
- B. There are no errors in the code that would cause a runtime error.
- C. The float variable x holds the integer value 97, not the ASCII representation of 'a'.
Tip: Remember that characters are stored as integers in memory.
Question 6Medium
What is the output of the following C code(on a 64 bit machine)?
#include <stdio.h>
union Sti
{
int nu;
char m;
};
int main()
{
union Sti s;
printf("%d", sizeof(s));
return 0;
}
- 8
- 5
- 9
- 4
Answer: D. 4
Explanation
A union is as large as its biggest member. Here the int is 4 bytes, so sizeof(s) is 4 and the program prints 4.
4
Step-by-step solution
- A union's size is the size of its largest member.
- Int takes 4 bytes on a 64-bit machine.
- Char takes 1 byte, but it's not the largest.
- Therefore, sizeof(s) = 4.
Why the other options are wrong
- A. 8 is too large for an int.
- B. 5 is larger than necessary.
- C. 9 is much larger than the actual size.
Tip: Remember: Union size = largest member size.
Question 7Medium
What will be output when you will execute following c code?
#include <stdio.h>
int main()
{
printf("%d\t",sizeof(6.5));
printf("%d\t",sizeof(90000));
printf("%d",sizeof('A'));
return 0;
}
(Assume Turbo C, a 16-bit compiler.)
- 8 2 1
- 4 4 1
- 8 4 1
- 8 4 2
Answer: D. 8 4 2
Explanation
Turbo C++ 3.0: 8 4 2
Turbo C ++4.5: 8 4 2
Linux GCC: 8 4 4
Visual C++: 8 4 4
By default data type of numeric constants is:
6.5 : double
90000 : long int
'A' : char
In C size of data type varies from compiler to compiler. In TURBO C 3.0 (16 bit compilers) size of:
- double is 8 byte
- Character constant is 2 byte (size of char data type is one byte)
In TURBO C 4.5 or Linux GCC compilers (32 bit compilers) size of:
- double is 8 byte, long int is 8 byte
- Character constant is 2 byte, Long int is 4 byte
Step-by-step solution
- sizeof(6.5) is the size of a double, which is 8 bytes.
- 90000 is too large for a 16-bit int, so it has type long, which is 4 bytes, and sizeof(90000) is 4.
- In C, the character constant 'A' has type int, so sizeof('A') is the size of int, which is 2 bytes on a 16-bit compiler such as Turbo C.
- The program prints the three values separated by tabs.
- So the output is 8 4 2.
Why the other options are wrong
- A. sizeof(90000) is 4, not 2, and 'A' has type int in C, so it is not 1 byte.
- B. 6.5 is a double, which is 8 bytes, not a 4-byte float.
- C. In C, 'A' has type int, so its size is not 1; the value 1 applies only in C++.
Tip: In C, a character constant such as 'A' has type int, so sizeof('A') equals sizeof(int); check the int size of the compiler.
Question 8Medium
Which of the datatypes have size that is variable?
int
struct
float
double
Answer: B.
Explanation
The size of a struct depends on its members and any padding, so it can differ from one struct to another. int, float and double have a fixed size for a given compiler.
Step-by-step solution
- A variable-size type is one whose size is not fixed by the type itself.
- Int, float and double have fixed sizes for a given platform.
- A struct's size is the total of its members plus padding, so it depends on its members.
- The answer is struct.
Why the other options are wrong
- A. Int has a fixed size on a given platform.
- C. Float has a fixed size on a given platform.
- D. Double has a fixed size on a given platform.
Tip: Only aggregate types such as struct depend on their contents for size.
Question 9Medium
Comment on the output of this C code..?
#include <stdio.h>
int main()
{
char c; int i = 0;
FILE *file;
file = fopen("test.txt", "w+");
fprintf(file, "%c", 'a');
fprintf(file, "%c", -1);
fprintf(file, "%c", 'b');
fclose(file);
file = fopen("test.txt", "r");
while ((c = fgetc(file)) != -1)
printf("%c", c);
return 0;
}
- a
- Infinite loop
- Depends on what fgetc returns
- Depends on the compiler
Answer: A. a
Explanation
The file receives a, then the byte for -1 (0xFF), then b. When the file is read, fgetc returns 0xFF, which becomes -1 in a signed char (as on most PC compilers), so the loop stops there. Only a is printed.
a
Step-by-step solution
- The program writes the bytes 'a', then -1 (stored as byte 0xFF), then 'b' into test.txt.
- When reading back, the first byte 'a' is printed.
- Fgetc returns 255 for the 0xFF byte, and on a signed char system c stores it as -1, which equals -1 and ends the loop.
- So only 'a' is printed, which is option A: a.
Why the other options are wrong
- B. The loop ends at the 0xFF byte on signed char systems, so it does not run forever.
- C. On signed char systems the result is fixed, so it does not depend on what fgetc returns.
- D. On the usual signed char systems the output does not change with the compiler.
Tip: Plain char is signed on most systems, so 0xFF reads back as -1 and looks like EOF.
Question 10Medium
Comment on the output of following C code?
#include <stdio.h>
int main()
{
float f1 = 0.1;
if (f1 == 0.1f)
printf("equal");
else
printf("not equal");
}
- equal
- not equal
- Output depends on compiler
- None of the mentioned
Answer: A. equal
Explanation
0.1f is a float, the same type as f1, so both hold the same value and the comparison is true. The program prints equal.
equal
Step-by-step solution
F1 is a float set to 0.1, which is rounded to the nearest float.0.1f is also the nearest float to 0.1, so both values are the same float.- The comparison
f1 == 0.1f is true, so the program prints equal. - So the output is equal.
Why the other options are wrong
- B. Equal floats compare equal, so not equal is wrong.
- C. Both sides are float, so the result does not depend on the compiler.
- D. The output is known, so none of the mentioned is wrong.
Tip: Be careful comparing floats, but here both sides round to the same float.