These are the c++/cpp objective questions related to coding. these objective questions consists of programs to test your knowledge on syntax's. so learn the concepts and check these objective questions to test and improve your programming and error debugging skills. I am sharing 10+ objective questions related to programs in cpp in this post. practice all these objective questions and improve your programming skills.
main()
{
struct xx
{
int x=3;
char name[]=”hello”;
};
struct xx *s=malloc(sizeof(struct xx));
printf(“%d”,s->x);
printf(“%s”,s->name);
}
Answer: Compiler Error
Explanation:
Initialization should not be done for structure members inside the structure declaration
main( )
{
printf(“%d”, out);
}
int out=100;
Answer:
Compiler error: undefined symbol out in function main.
Explanation:
The rule is that a variable is available for use from the point of declaration. Even though a is a global variable, it is not available for main. Hence an error.
main()
{
char string[]=”Hello World”;
display(string);
}
void display(char *string)
{
printf(“%s”,string);
}
Answer:
Compiler Error : Type mismatch in redeclaration of function display
Explanation :
In third line, when the function display is encountered, the compiler doesn’t know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.
#define int char
main()
{
int i=65;
printf(“sizeof(i)=%d”,sizeof(i));
}
Answer: sizeof(i)=1
Explanation:
Since the #define replaces the string int by the macro char
main()
{
int i=10;
i=!i>14;
Printf (“i=%d”,i);
}
Answer: i=0
Explanation:
In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).
main()
{
printf(“\nab”);
printf(“\bsi”);
printf(“\rha”);
}
Answer: hai
Explanation:
\n – newline \b – backspace \r – linefeed(previous line)
main()
{
int i;
printf(“%d”,scanf(“%d”,&i)); // value 10 is given as input here
}
Answer: 1
Explanation:
Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.
void main()
{
int const * p=5;
printf(“%d”,++(*p));
}
Answer: Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a “constant integer”. But we tried to change the value of the “constant integer”.
What will be output if you will compile and execute the following c code?
#include
void main()
{
char *str;
scanf("%[^\n]",str);
printf("%s",str);
}
(a)It will accept a word as a string from user.
(b)It will accept a sentence as a string from user.
(c)It will accept a paragraph as a string from user.
(d)Compiler error
Ans: (b)
Explanation:
Task of % [^\t] is to take the stream of characters until it doesn’t receive new line character ‘\n’ i.e. enter button of your keyboard.
What will be output if you will compile and execute the following c code?
#include
void main()
{
int array[3]={5};
int i;
for(i=0;i<=2;i++)
printf("%d ",array[i]);
}
(a)5 garbage garbage (b)5 0 0
(c)5 null null (d)Compiler error
Ans: (b)
Explanation:
Storage class of an array which initializes the element of the array at the time of declaration is static. Default initial value of static integer is zero.
What will be output if you will compile and execute the following c code? #include
void main( )
{
register int i,x;
scanf("%d",&i);
x=++i + ++i + ++i;
printf("%d",x);
}
(a)17 (b)18 (c)21 (d)Compiler error
Output: (d)
Explanation:
In c register variable stores in CPU it doesn’t store in RAM. So register variable have not any memory address. So it is illegal to write &a.
What will be output if you will compile and execute the following c code?
#include
void main(){
int a=-20;
int b=-3;
printf("%d",a%b);
}
(a)2 (b)-2 (c)18 (d)-18
Output: (b)
Explanation:
Sign of resultant of modular division depends upon only the sign of first operand.
#include
int main()
{
goto abc;
printf("main");
}
void dispaly()
{
abc:
printf("display");
}
(a)main (b)display (c)maindisplay (d)Compiler error
Output: (d)
Explanation:
Label of goto cannot be in other function because control cannot move from one function to another function directly otherwise it will show compiler error: unreachable label
#include
int main()
{
const int i=5;
i++;
printf("%d",i);
}
(a)5 (b)6 (c)0 (d)Compiler error
Output: (d)
Explanation:
We cannot modify the const variable by using increment operator.
#include
int main()
{
int array[2][2][3]={0,1,2,3,4,5,6,7,8,9,10,11};
printf("%d",array[1][0][2]);
}
(a)4 (b)5 (c)6 (d)7 (e)8
Output: e
Explanation:
array[1][0][2] means 1*(2*3)+0*(3)+3=9th element of array starting from zero i.e. 8.
#include
void main()
{
int a=5,b=10,c=1;
if(a&&b>c)
printf("codershunt");
else
break;
}
output: error Misplaced break.
Explanation:
Keyword break is not syntactical part of if else statement. So we cannot use break keyword in if else statement. This keyword can be use in case of loop or switch case statement. Hence when you will compile above code compiler will show an error message: Misplaced break.
#include
void main()
{
if(!printf("Mukesh Ambani"))
if(printf(" Lakashmi Mittal"))
}
Output: Mukesh Ambani
Explanation:
Return type of printf function is int. This function return a integral value which is equal to number of characters a printf function will print on console. First of all printf function will print Mukesh Ambani. Since it is printing 13 character so it will return 13. So,
!printf("Mukesh Ambani")
= !13= 0
In c language zero represents false. So if(0) is false so next statement which inside thebody of first if statement will not execute.
#include
void main()
{
int x=1;
if(x)
printf("The Godfather");
x;
else
printf("%d",x);
}
Output: error: Misplace else
Explanation:
If you are not using { and } in if clause then you can write only one statement. Otherwiseit will cause of compilation error: Misplace else
#include
void main()
{
if('\0');
else if(NULL)
printf("codershunt");
else;
}
output: no output
Explanation:
‘\0’ is null character constant. Its ASCII value is zero. if(0) means false so program control will check else if clause. NULL is macro constant which has been defined in stdio.h which also returns zero.
main()
{
char *p;
p="Hello";
printf("%c\n",*&*p);
}
Answer: H
Explanation:
* is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so itsvalue is H. Again & references it to an address and * dereferences it to the value H.
click here for more objective type questions on c programming.
Recommended for you :-
Complete C Programming Tutorials
C Programming Exercises
C Solved Programs
C Programming Useful Tips
C Objective Questions on Coding to Practice for beginners :-
1)
#includemain()
{
struct xx
{
int x=3;
char name[]=”hello”;
};
struct xx *s=malloc(sizeof(struct xx));
printf(“%d”,s->x);
printf(“%s”,s->name);
}
Answer: Compiler Error
Explanation:
Initialization should not be done for structure members inside the structure declaration
2)
{
printf(“%d”, out);
}
int out=100;
Answer:
Explanation:
The rule is that a variable is available for use from the point of declaration. Even though a is a global variable, it is not available for main. Hence an error.
3)
{
char string[]=”Hello World”;
display(string);
}
void display(char *string)
{
printf(“%s”,string);
}
Answer:
Compiler Error : Type mismatch in redeclaration of function display
Explanation :
In third line, when the function display is encountered, the compiler doesn’t know anything about the function display. It assumes the arguments and return types to be integers, (which is the default type). When it sees the actual function display, the arguments and type contradicts with what it has assumed previously. Hence a compile time error occurs.
4)
main()
{
int i=65;
printf(“sizeof(i)=%d”,sizeof(i));
}
Answer: sizeof(i)=1
Explanation:
Since the #define replaces the string int by the macro char
5)
{
int i=10;
i=!i>14;
Printf (“i=%d”,i);
}
Answer: i=0
Explanation:
In the expression !i>14 , NOT (!) operator has more precedence than ‘ >’ symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is false (zero).
6)
{
printf(“\nab”);
printf(“\bsi”);
printf(“\rha”);
}
Answer: hai
Explanation:
7)
main()
{
int i;
printf(“%d”,scanf(“%d”,&i)); // value 10 is given as input here
}
Answer: 1
Explanation:
Scanf returns number of items successfully read and not 1/0. Here 10 is given as input which should have been scanned successfully. So number of items read is 1.
8)
{
int const * p=5;
printf(“%d”,++(*p));
}
Answer: Compiler error: Cannot modify a constant value.
Explanation:
p is a pointer to a “constant integer”. But we tried to change the value of the “constant integer”.
(9)
#include
void main()
{
char *str;
scanf("%[^\n]",str);
printf("%s",str);
}
(a)It will accept a word as a string from user.
(b)It will accept a sentence as a string from user.
(c)It will accept a paragraph as a string from user.
(d)Compiler error
Ans: (b)
Explanation:
Task of % [^\t] is to take the stream of characters until it doesn’t receive new line character ‘\n’ i.e. enter button of your keyboard.
(10)
#include
void main()
{
int array[3]={5};
int i;
for(i=0;i<=2;i++)
11)
void main( )
{
register int i,x;
scanf("%d",&i);
x=++i + ++i + ++i;
printf("%d",x);
}
(a)17 (b)18 (c)21 (d)Compiler error
Output: (d)
Explanation:
In c register variable stores in CPU it doesn’t store in RAM. So register variable have not any memory address. So it is illegal to write &a.
(12)
#include
void main(){
int a=-20;
int b=-3;
printf("%d",a%b);
}
(a)2 (b)-2 (c)18 (d)-18
Output: (b)
Explanation:
Sign of resultant of modular division depends upon only the sign of first operand.
13)
int main()
{
goto abc;
printf("main");
}
void dispaly()
{
abc:
printf("display");
}
(a)main (b)display (c)maindisplay (d)Compiler error
Output: (d)
Explanation:
Label of goto cannot be in other function because control cannot move from one function to another function directly otherwise it will show compiler error: unreachable label
14)
int main()
{
const int i=5;
i++;
printf("%d",i);
}
(a)5 (b)6 (c)0 (d)Compiler error
Output: (d)
Explanation:
We cannot modify the const variable by using increment operator.
(15)
int main()
{
int array[2][2][3]={0,1,2,3,4,5,6,7,8,9,10,11};
printf("%d",array[1][0][2]);
}
(a)4 (b)5 (c)6 (d)7 (e)8
Output: e
Explanation:
array[1][0][2] means 1*(2*3)+0*(3)+3=9th element of array starting from zero i.e. 8.
16)
void main()
{
int a=5,b=10,c=1;
if(a&&b>c)
printf("codershunt");
else
break;
}
output: error Misplaced break.
Explanation:
Keyword break is not syntactical part of if else statement. So we cannot use break keyword in if else statement. This keyword can be use in case of loop or switch case statement. Hence when you will compile above code compiler will show an error message: Misplaced break.
17)
void main()
{
if(!printf("Mukesh Ambani"))
if(printf(" Lakashmi Mittal"))
}
Output: Mukesh Ambani
Explanation:
Return type of printf function is int. This function return a integral value which is equal to number of characters a printf function will print on console. First of all printf function will print Mukesh Ambani. Since it is printing 13 character so it will return 13. So,
!printf("Mukesh Ambani")
= !13= 0
In c language zero represents false. So if(0) is false so next statement which inside thebody of first if statement will not execute.
18)
void main()
{
int x=1;
if(x)
printf("The Godfather");
x;
else
printf("%d",x);
}
Output: error: Misplace else
Explanation:
If you are not using { and } in if clause then you can write only one statement. Otherwiseit will cause of compilation error: Misplace else
19)
void main()
{
if('\0');
else if(NULL)
printf("codershunt");
else;
}
output: no output
Explanation:
‘\0’ is null character constant. Its ASCII value is zero. if(0) means false so program control will check else if clause. NULL is macro constant which has been defined in stdio.h which also returns zero.
20)
{
char *p;
p="Hello";
printf("%c\n",*&*p);
}
Answer: H
Explanation:
* is a dereference operator & is a reference operator. They can be applied any number of times provided it is meaningful. Here p points to the first character in the string "Hello". *p dereferences it and so itsvalue is H. Again & references it to an address and * dereferences it to the value H.
More C Objective Questions related to other concepts :-
There are more c objective questions related to other concepts. learn all those concepts and improve your programming skills.click here for more objective type questions on c programming.
Recommended for you :-
Complete C Programming Tutorials
C Programming Exercises
C Solved Programs
C Programming Useful Tips
joZ!
ReplyDeleteThanks for posting this great post. Do have a nice day...
ReplyDelete√
√
Check.. Twerk it VPN 4g app download
*
All coming from W3HowTo.com
This is my best game so far, I do play it day and night after downloading the Pokémon Go app apk online.
.
But I need to improve my ability in playing the android game...I wonder if this will turn out one day to become the best Xbox game
.
Thanks for this updating this post!! Do have a nice day.
My regards,
Joseph
W3HowTo.com
very informative article thanks for sharing
ReplyDelete