Advance C Programming100-Ques
Advance C Programming100-Ques
Answer: Option C
#include<stdio.h>
#include<stdlib.h>
int main()
{
unsignedchar;
FILE *fp;
fp=fopen("trial", "r");
if(!fp)
{
printf("Unable to open file");
exit(1);
}
fclose(fp);
return0;
}
Answer: Option C
Explanation:
This program tries to open the file trial.txt in read mode. If file not
exists or unable to read it prints "Unable to open file" and then
terminate the program.
If file exists, it simply close the file and then terminates the program.
3. Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *ptr;
*ptr = (char)malloc(30);
strcpy(ptr, "RAM");
printf("%s", ptr);
free(ptr);
return0;
}
C. Error: in free(ptr);
D. No error
Answer: Option B
Explanation:
#include<stdio.h>
int main()
{
inti=0;
for(; i<=5; i++);
printf("%d", i);
return0;
}
A. 0, 1, 2, 3, 4, 5 B. 5
C. 1, 2, 3, 4 D. 6
Answer: Option D
Explanation:
Loop 1: here i=0, the condition in for(; 0<=5; i++) loop satisfies and
then i is incremented by '1'(one)
Loop 2: here i=1, the condition in for(; 1<=5; i++) loop satisfies and
then i is incremented by '1'(one)
Loop 3: here i=2, the condition in for(; 2<=5; i++) loop satisfies and
then i is incremented by '1'(one)
Loop 4: here i=3, the condition in for(; 3<=5; i++) loop satisfies and
then i is increemented by '1'(one)
Loop 5: here i=4, the condition in for(; 4<=5; i++) loop satisfies and
then i is incremented by '1'(one)
Loop 6: here i=5, the condition in for(; 5<=5; i++) loop satisfies and
then i is incremented by '1'(one)
Loop 7: here i=6, the condition in for(; 6<=5; i++) loop fails and then i is
not incremented.
#include<stdio.h>
int main()
{
inti;
i = printf("How r u\n");
i = printf("%d\n", i);
printf("%d\n", i);
return0;
}
How r u How r u
A. 7 B. 8
2 2
How r u
Error: cannot
C. 1 D.
assign printf to variable
1
Answer: Option B
Explanation:
i = printf("How r u\n"); This line prints "How r u" with a new line
character and returns the length of string printed then assign it
to variable i.
So i = 8 (length of '\n' is 1).
#include<stdio.h>
int main()
{
int a = 500, b = 100, c;
if(!a >= 400)
b = 300;
c = 200;
printf("b = %d c = %d\n", b, c);
return0;
}
Explanation:
1. !
2. sizeof
3. ~
4. &&
A. 1, 2 B. 1, 3
C. 2, 4 D. 1, 2, 3
Answer: Option D
Explanation:
#include<stdio.h>
int main()
{
inti=-3, j=2, k=0, m;
m = ++i&& ++j && ++k;
printf("%d, %d, %d, %d\n", i, j, k, m);
return0;
}
A. -2, 3, 1, 1 B. 2, 3, 1, 2
C. 1, 2, 3, 1 D. 3, 3, 1, 2
Answer: Option A
Explanation:
#include<stdio.h>
int reverse(int);
int main()
{
int no=5;
reverse(no);
return0;
}
int reverse(int no)
{
if(no == 0)
return0;
else
printf("%d,", no);
reverse (no--);
}
A. Print 5, 4, 3, 2, 1 B. Print 1, 2, 3, 4, 5
Explanation:
#include<stdio.h>
#include<stdlib.h>
int main()
{
inti=0;
i++;
if(i<=5)
{
printf("Informatrix”);
exit(1);
main();
}
return0;
}
C. Infinite loop
D. Prints "Informatrix"
Answer: Option D
Explanation:
A. /+*-
B. *-/+
C. +-/*
D. /*+-
Ans Option D
Explanation:
Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction).
How Do I Remember ? BODMAS !
• B - Brackets first
• O - Orders (ie Powers and Square Roots, etc.)
• DM - Division and Multiplication (left-to-right)
• AS - Addition and Subtraction (left-to-right)
A. A
B. a
C. c
D. 65
Answer: Option A
Explanation:
Step 1: char p[] = "%d\n"; The variable p is declared as an array of characters and initialized
with string "%d".
Step 2: p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array
pbecomes "%c".
Step 3: printf(p, 65); becomes printf("%c", 65);
Therefore it prints the ASCII value of 65. The output is 'A'.
#include<stdio.h>
int get();
int main()
{
const int x = get();
printf("%d", x);
return 0;
}
int get()
{
return 20;
}
A.Garbage value
B.Error
C.20
D.0
Answer: Option C
Explanation:
Step 1: int get(); This is the function prototype for the funtion get(), it tells the compiler
returns an integer value and accept no parameters.
Step 2: const int x = get(); The constant variable x is declared as an integer data type and
initialized with the value "20".
25. How many times the while loop will get executed if a short int is 2 byte wide?
#include<stdio.h>
int main()
{
int j=1;
while(j <= 255)
{
printf("%c %d\n", j, j);
j++;
}
return 0;
}
A.Infinite times
B.255 times
C.256 times
D.254 times
Answer: Option B
Explanation:
The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide)
does not affect the while() loop.
26. How many times the below loop will get executed?
#include<stdio.h>
main()
{
int i,j;
i = 10;
for (j=i==10 ; j<=10 ; j++)
{
printf("\n%d",j);
}
}
(A) 1
(B) 10
(C) 11
(D) Compilation Error
Answer : 10
Explanation: Expression i ==10 return 1 and j get initialized to 1.
#include "stdio.h"
extern int a;
main(){
printf("\na=%d",a);
return 0;
}
(A) a=0
(B) a=garbage value
(C) error
(D) none of these
Answer : error
Explanation: Linking undefined symbol.
Answer: b
Answer: a
Answer: a
Explanation: 64-8
Answer: c
Explanation: Decimal to Hexa conversion:
100/16 Quotient 6; Remainder 4
6/16 Quotient 0; Remainder 6
Writing the remainders in down to upward direction we get 64
Decimal to Octal conversion:
100/8 Quotient 12; Remainder 4
12/8 Quotient 1; Remainder 4
1/8 Quotient 0; Remainder 1
Writing the remainders in down to upward direction we get 144
Answer: b
Explanation: Compiler executes right to left.
Answer: d
47. Default storage class if not any is specified for a local variable, is auto
a) true
b) false
c) Depends on the standard
d) None of the mentioned
Answer: a
printf("%f\n", sqrt(36.0)); It prints the square root of 36 in the float format(i.e 6.000000).
Declaration Syntax: double sqrt(double x) calculates and return the positive square root of
the given number.
Answer:c
a.Nothing
b.nn /n/n nn
c.nn /n/n
d.Error
Show/Hide Answer
Answer = B
51. How many times will the following loop be executed?
ch = 'b';
while(ch >= 'a' && ch <= 'z')
A. 0 B. 25
C. 26 D. 1
View Answer & Explanation
Ans : B
52. Consider the following program fragment
switch(input)
{
case '1':
printf("One");
case '2':
printf("Two");
case '3':
printf(""Three");
default:
Printf("Default");
break;
}What will be printed when input is 2?
A. 7 B. 5
C. 2 D. None of the above
View Answer & Explanation
Ans : A
54.What is the output of the below code snippet?
#include<stdio.h>
main()
{
int a = 5, b = 3, c = 4;
C - a=5, b=3, 0
D - compile error
Answer : A
#include<stdio.h>
main()
{
int a[3] = {2,1};
printf("%d", a[a[1]]);
}
A-0
B-1
C-2
D-3
Answer : B
#include<stdio.h>
#include<math.h>
int main()
{
float n=1.54;
printf("%f, %f\n", ceil(n), floor(n));
return 0;
}
A. 2.000000, 1.000000
B. 1.500000, 1.500000
C. 1.550000, 2.000000
D. 1.000000, 2.000000
Answer: Option A
58. What function should be used to free the memory allocated by calloc() ?
A. dealloc();
B. malloc(variable_name, 0)
C. free();
D. memalloc(variable_name, 0)
Answer: Option C
59.. Point out the correct statement which correctly free the memory pointed to by 's' and
'p' in the following program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
struct ex
{
int i;
float j;
char *s
};
struct ex *p;
p = (struct ex *)malloc(sizeof(struct ex));
p->s = (char*)malloc(20);
return 0;
}
A. free(p); , free(p->s);
B. free(p->s); , free(p);
C. free(p->s);
D. free(p);
Answer: Option B
60. Which statement will you add in the following program to work it correctly?
#include<stdio.h>
int main()
{
printf("%f\n", log(36.0));
return 0;
}
A. #include<conio.h>
B. #include<math.h>
C. #include<stdlib.h>
D. #include<dos.h>
Answer: Option B
61. In a file contains the line "I am a boy\r\n" then on reading this
line into the array strusing fgets(). What will str contain?
A. "I am a boy\r\n\0" B. "I am a boy\r\0"
Answer: Option C
Explanation:
fgets reads characters from stream into the string s. It stops when it
reads either n - 1 characters or a newline character, whichever comes
first.