Professional Documents
Culture Documents
Additional C Questions
Additional C Questions
If invalid, state
the reason.
(a) sample1 (b) 5sample (c) data_7 (d) return (e) #fine (f) variable
(g) 91-080-100 (h) name & age (i) _val (j) name_and_age
[Q002] Determine which of the following are VALID character constants. If
invalid, state the reason.
(a) 'y' (b) '\r' (c) 'Y' (d) '@' (e) '/r' (f) 'word' (g) '\0'
(h) '\?' (i) '\065' (j) '\'' (k) ' '
[Q003] Determine which of the following are VALID string constants. If invalid,
state the reason.
(a) 'Hi Friends' (b) "abc,def,ghi" (c) "Qualification (d) "4325.76e-8" (e)
"Don\'t sleep" (f) "He said, "You\'re great" (g) "" (h) " " (i) "Rs.100/-"
[Q004] Determine which of the following numerical values are valid constants. If
a constant is valid, specify whether it is integer or real. Also, specify the
base for each valid integer constant.
(a) 10,500 (b) 080 (c) 0.007 (d) 5.6e7 (e) 5.6e-7 (f) 0.2e-0.3
(g) 0.2e 0.3 (h) 0xaf9s82 (i) 0XABCDEFL (j) 0369CF (k) 87654321l (l) 87654321
[Q005] Determine which of the following floating-point constants are VALID for
the quantity (5*100000).
(a) 500000 (b) 0.5e6 (c) 5E5 (d) 5e5 (e) 5e+5 (f) 500E3 (g) .5E6
(h) 50e4 (i) 50.E+4 (j) 5.0E+5 (k) All of the above (l) None of these
[Q006] What will be the output of the following program :
int main()
{
printf("%f",123.);
return(0);
}
(a)123 (b)Compile-Time Error (c)123.00 (d)123.000000 ans:: d
[Q007] What will be the output of the following program :
int main()
{
printf("%d",sizeof(integer));
return(0);
}
(a)2 (b)Compile-Time Error(c)4 (d)None of these ans:: b
[Q008] What will be the output of the following program :
int main()
{
char str[]="C For Swimmers";
printf("%d",sizeof str);
return(0);
}
(a)14 (b)Compile-Time Error (c)15 (d)None of these ans:: b
[Q009] What will be the output of the following program :
int main()
{
char str[]="C For Swimmers";
printf("%d",++(sizeof(str)));
return(0);
}
(a)14 (b)Compile-Time Error (c)15 (d)None of these ans:: b
[Q010] What will be the output of the following program :
int main()
{
char str[]="C For Swimmers";
printf("%d",-sizeof(str));
return(0);
}
(a)14 (b)Compile-Time Error (c)-15 (d)-14 ans:: c
[Q011] What will be the output of the following program :
int main()
{
printf("%d",!(100==100)+1);
return(0);
}
(a)100 (b)0 (c)1 (d)2 ans:: c
[Q012] What will be the output of the following program :
int main()
{
int x=5,y=6,z=2;
z/=y/z==3?y/z:x*y;
printf("%d",z);
return(0);
}
(a)Compile-Time Error (b)2 (c)0 (d)1
[Q013] What will be the output of the following program :
int main()
{
printf("%d %d %d",5,!5,25-!25);
return(0);
}
(a)5 10 22 (b)5 5 25 (c)5 0 25 (d)5 1 24
[Q014] What will be the output of the following program :
int main()
{
int a=500,b=100,c=30,d=40,e=19;
a+=b-=c*=d/=e%=5;
printf("%d %d %d %d %d",a,b,c,d,e);
return(0);
}
(a)500 100 30 40 4 b)Run-Time Error c)700 200 300 10 4 d)300 -200 300 10 4
[Q015] What will be the output of the following program :
int main()
{
int a=500,b=100,c=30,d=40,e=19;
if((((a > b) ? c : d) >= e) && !((e <= d) ? ((a / 5) == b) : (c == d)))
printf("Success");
else
printf("Failure");
return(0);
}
(a)Success (b)Failure (c)Invalid Statement(s) (d)None of these
[Q016] What will be the output of the following program :
int main()
{
int a=1,b=2,c=3,d=4;
printf("%d",!a?b?!c:!d:a);
return(0);
}
(a)1 (b)2 (c)3 (d)4
[Q017] What will be the output of the following program :
int main()
{
int i=12345,j=-13579,k=-24680;
long ix=123456789;
short sx=-2222;
unsigned ux=5555;
printf("\n%d %d %d %ld %d %u",i,j,k,ix,sx,ux);
printf("\n\n%3d %3d %3d\n%3ld %3d %3u",i,j,k,ix,sx,ux);
printf("\n\n%8d %8d %8d\n%15ld %8d %8u",i,j,k,ix,sx,ux);
printf("\n\n%-8d %-8d\n%-8d %-15ld\n%-8d %-8u",i,j,k,ix,sx,ux);
printf("\n\n%+8d %+8d\n%+8d %+15ld\n%+8d %8u",i,j,k,ix,sx,ux);
printf("\n\n%08d %08d\n%08d %015ld\n%08d %08u",i,j,k,ix,sx,ux);
return(0);
}
[Q018] What will be the output of the following program :
int main()
{
int i=12345,j=0xabcd9,k=077777;
printf("%d %x %o",i,j,k);
printf("\n%3d %3x %3o",i,j,k);
printf("\n%8d %8x %8o"i,j,k);
printf("\n%-8d %-8x %-8o",i,j,k);
printf("\n%+8d %+8x %+8o",i,j,k);
printf("\n%08d %#8x %#8o",i,j,k);
return(0);
}
[Q019] What will be the output of the following program :
int main()
{
char c1='A', c2='B', c3='C';
printf("%c %c %c",c1,c2,c3);
printf("\n%c%c%c",c1,c2,c3);
printf("\n%3c %3c %3c",c1,c2,c3);
printf("\n%3c%3c%3c",c1,c2,c3);
printf("\nc1=%c c2=%c c3=%c",c1,c2,c3);
return(0);
}
[Q020] What will be the output of the following program :
int main()
{
float a=2.5, b=0.0005, c=3000.;
printf("%f %f %f",a,b,c);
printf("\n%3f %3f %3f",a,b,c);
printf("\n%8f %8f %8f",a,b,c);
printf("\n%8.4f %8.4f %8.4f",a,b,c);
printf("\n%8.3f %8.3f %8.3f",a,b,c);
printf("\n%e %e %e",a,b,c);
printf("\n%3e %3e %3e",a,b,c);
printf("\n%12e %12e %12e",a,b,c);
printf("\n%8.2e %8.2e %8.2e",a,b,c);
printf("\n%-8f %-8f %-8f",a,b,c);
printf("\n%+8f %+8f %+8f",a,b,c);
printf("\n%08f %08f %08f",a,b,c);
printf("\n%#8f %#8f %#8f",a,b,c);
printf("\n%g %g %g",a,b,c);
printf("\n%#g %#g %#g"a,b,c);
return(0);
}
[Q021] What will be the output of the following program :
int main()
{
char str[]="C For Swimmers";
printf("%s",str);
printf("\n%.5s",str);
printf("\n%8.*s",5,str);
printf("\n%-10s %.1s",str+6,str);
return(0);
}
[Q022] What will be the output of the following program [NOTE : 3 values entered
by the user are:100 200 300] :
int main()
{
int a=1,b=2,c=3;
scanf("%d %*d %d",&a,&b,&c);
printf("a=%d b=%d c=%d",a,b,c);
return(0);
}
(a)1 2 3 (b)100 200 300 (c)100 200 3 (d)100 300 3
[Q023] What will be the output of the following program [NOTE : THE USER INPUT
IS:Dear Friends, What is the output?] :
int main()
{ char line[80]; // Max. length=80 Chars
scanf("%[^,]s",line);
printf("\n%s",line);
return(0);
}(a)Compile-Time Error (b)Dear Friends (c)What is the output?
(d)None of these
[Q024] What will be the output of the following program [NOTE : THE USER INPUT IS
:A B C] :
int main()
{ char a,b,c;
scanf("%c%c%c",&a,&b,&c);
printf("a=%c b=%c c=%c",a,b,c);
return(0); }
a)a=A b=B c=C(b)a=A b= c=B (c)a=A b= c=C (d)None of these
[Q025] What will be the output of the following program [NOTE : THE USER INPUT
IS:5 5.75] :
int main()
{ int i=1;
float f=2.25;
scanf("%d a %f",&i,&f);
printf("%d %.2f",i,f);
return(0); }
a)1 2.25 (b)5 5.75 (c)5 2.25 (d)None of these
[Q026] What will be the output of the following program [NOTE : THE USER INPUT
IS :ABC DEF GHI] :
int main()
{ char a,b,c;
scanf("%c %c %c",&a,&b,&c);
printf("a=%c b=%c c=%c",a,b,c);
return(0); }
(a)a=ABC b=DEF c=GHI (b)a=A b=B c=C (c)a=A b=D c=G (d)None of these
[Q027] What will be the output of the following program [NOTE : THE USER INPUT
IS:CMeansSea Ocean Vast] :
int main()
{ char a[80],b[80],c[80];
scanf("%1s %5s %3s",a,b,c);
printf("%s %s %s",a,b,c);
return(0); }
(a)C O V (b)C Means Sea (c)C Ocean Vas (d)None of these
[Q028] What will be the output of the following program [NOTE : THE USER INPUT
IS :123456 44 544] :
int main()
{ int a,b,c;
scanf("%1d %2d %3d",&a,&b,&c);
printf("Sum=%d",a+b+c);
return(0); }
(a)Sum=480 (b)Sum=594 (c)Sum=589 (d)None of these
[Q018] What are the largest values that can be assigned to each of the bit
fields defined in [Q017] above.
(a)a=0 b=2 c=3 d=4 (b)a=1 b=2 c=7 d=15
(c)a=1 b=3 c=7 d=15 (d)None of these
Q001] The following code is not well-written. What does the program do ?
int
main(
){int a=1,
b=2,c=3,d=4;printf("%d %d",
a,b);printf(
" %d %d",c,d);
return(0); }
(a)Run-Time Error (b)Compile-Time Error (c)1 2 3 4 (d)None of these
[Q002] What will be the output of the following program :
int main()
{ int a=1,b=2,c=3;
c=(--a,b++)-c;
printf("%d %d %d",a,b,c);
return(0); }
(a)0 3 -3 (b)Compile-Time Error (c)0 3 -1 (d)0 3 0
Q001] The following code is not well-written. What does the program do ?
int main()
{int a=1, b=2,c=3,d=4;
printf("%d %d", a,b);
printf( " %d %d",c,d);
return(0); }
(a)Run-Time Error (b)Compile-Time Error (c)1 2 3 4 (d)None of these
[Q002] W.A.P. in C to find the Fifth root of the sum of the squares of the first
100 ODD numbers only.
/* To find the Fifth root of the sum of the squares of the first 100 ODD numbers
ONLY */
#include< stdio.h >
#include< conio.h >
#include< math.h >
int main(void)
{ long i,oddnum=1,sqrnum,sum=0;
for (i=1; i<=100; i++)
{ sqrnum=oddnum * oddnum; // Square the ODD number
sum+=sqrnum; // Add Square value to the sum
oddnum+=2; // Get the next ODD number
}printf("\nThe result is : %ld,%.2f",sum,pow((double)sum,(1.0/5.0)));
return(0);
} /* End of Main */
[Q004] W.A.P. in C to check whether given number x is equal to the value 2 POWER
i or something, where i>=0 using BITWISE operators ONLY. [Hint : Check whether
the given number x is equal to the value 2 POWER i or something using BITWISE
operators ONLY]
/* Check whether the given number x is equal to the value 2 power i or not
using BITWISE operators ONLY */
#include< stdio.h >
#include< conio.h >
int main(void)
{ long x;
printf("Enter a number : ");
scanf("%ld",&x);
if ((x & 0x1) == 0x0) // Checking the Least significant bit
printf("The given number %ld is EQUAL to the value 2 POWER something",x);
else
printf("The given number %ld is NOT EQUAL to the value 2 POWER
something",x);
getch();
return(0);
} /* End of Main */
[Q005] W.A.P. in C to maintain 2 STACKS within a SINGLE ARRAY and the values of
one stack should not overwrite the values of another stack.
/* Maintaining TWO STACKS within a SINGLE ARRAY */
#include< stdio.h >
#include< conio.h >
#define MAX 10
int stack[MAX],top1,top2;
void init()
{ top1=-1;
top2=10;}
void Push1(int item)
{ stack[++top1]=item;}
void Push2(int item)
{ stack[--top2]=item;}
int Pop1()
{ return stack[top1--]; }
int Pop2()
{ return stack[top2++];}
void Display()
{ int i;
if(top1==-1)
printf("\nStack1 : Empty");
else
{ printf("\nContent of Stack1 :\n");
for(i=0;i<=top1;i++)
printf("%d\t",stack[i]);
}
if(top2==10)
printf("\nStack2 : Empty");
else
{ printf("\nStack2 contains:\n");
for(i=MAX-1;i>=top2;i--)
printf("%d\t",stack[i]);
}
}
int main()
{ int item,ch;
init();
while(1)
{printf("\n\n\tMenu\n1.Push1\n2.Push2\n3.Pop1\n4.Pop2\n5.Display\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{ case 1 : if ((top1 + 1) < top2)
{
printf("\nEnter item to Push into Stack1:");
scanf("%d",&item);
Push1(item); }
else
printf("\nMemory is Full. Overflow Error");
break;
case 2 : if ((top2 - 1) > top1)
{ printf("\nEnter item to Push into Stack2:");
scanf("%d",&item);
Push2(item);
}
else
printf("\nMemory is Full. Overflow Error");
break;
case 3 : if(top1 <= -1)
printf("\nError : Underflow on pop1");
else
printf("\nPopped item from stack1 is : %d",Pop1());
break;
case 4 : if(top2 >= 10)
printf("\nError : Underflow on pop2");
else
printf("\nPopped item from stack2 is : %d",Pop2());
break;
case 5 : Display();
break;
case 6 : exit(0);
default: printf("\nInvalid Choice");
} }
return(0);
} /* End of Main */
[Q006] W.A.P. in C that act as a guessing game in which the user has eight tries
to guess a
randomly generated number. The program will tell the user each time whether he
guessed high or
low. The user WINS the game when the number guessed is same as randomly generated
number.
/* Guessing Game Solution */
#include< stdlib.h >
#include< stdio.h >
#include< time.h >
int main(void)
{ int i=8,rval,val,flag=1;
randomize(); // Initialize the random number generator
rval=random (100); // Generates a random number in the range 0 to 99
printf("Welcome to Guessing Game.\n");
printf("RULES:\n1. Only 8 chances to guess the randomly generated number.");
printf("\n2. You can WIN the game when the number guessed is same as the");
printf("randomly generated number.\n3. Follow the hints.");
printf("\n\n$$$ Good Luck. Start Guessing $$$");
for (i=1; i<=8; i++)
{ printf("\n\nGUESS %d ? ",i);
scanf("%d",&val);
if (val > rval)
printf("Your value is GREATER THAN the randomly generated number");
else if (val < rval)
printf("Your value is LESSER THAN the randomly generated number");
else
{ flag=1;
break; }
}
if (flag)
printf("\n\n*** You are the WINNER. No. of tries = %d ***",i);
else
printf("\n\n*** You are the LOSER. ***");
return(0);
} /* End of Main */
[Q007] W.A.P. to determine how much money is in a piggy bank that contains
several 50 paise coins, 25 paise coins, 20 paise coins, 10 paise coins and 5
paise coins. Use the following values to test your program : Five 50 paise coins,
Three 25 paise coins, Two 20 paise coins, One 10 paise coin and Fifteen 5 paise
coins. (Answer : Rs. 4.50)
/* To determine how much money in a piggy bank */
#include< stdio.h >
#include< string.h >
#include< conio.h >
int main(void)
{ float coin1=0.50,coin2=0.25,coin3=0.20,coin4=0.10,coin5=0.05,total=0.0;
int ncoins;
clrscr();
printf("How many 50 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin1);
printf("** %.2f **",total);
printf("\nHow many 25 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin2);
printf("** %.2f **",total);
printf("\nHow many 20 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin3);
printf("** %.2f **",total);
printf("\nHow many 10 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin4);
printf("** %.2f **",total);
printf("\nHow many 5 paise coins : ");
scanf("%d",&ncoins);
total += (ncoins * coin5);
printf("\n\nThe total amount is Rs.%.2f",total);
getch();
return(0);
} /* End of Main */
[Q008] Modify the program given in [Q007] to accept total amount (in rupees) and
convert them into paise.(Vice-versa of [Q007])
/* Denominations */
#include< stdio.h >
#include< string.h >
#include< conio.h >
int main(void)
{ int nc1,nc2,nc3,nc4,nc5,temp;
float total;
clrscr();
printf("Enter the amount : ");
scanf("%f",&total);
temp = total * 100;
nc1 = temp / 50;
temp = temp % 50;
nc2 = temp / 25;
temp = temp % 25;
nc3 = temp / 20;
temp = temp % 20;
nc4 = temp / 10;
temp = temp % 10;
nc5=temp;
printf("\n\nNo. of 50 paise coins = %d",nc1);
printf("\nNo. of 25 paise coins = %d",nc2);
printf("\nNo. of 20 paise coins = %d",nc3);
printf("\nNo. of 10 paise coins = %d",nc4);
printf("\nNo. of 5 paise coins = %d",nc5);
getch();
return(0);
} /* End of Main */
[Q09] W.A.P. in C to determine how many of the characters are vowels and how many
are consonants in a given line of text. Also terminate the string when the input
character encountered is other than the alphabets(a-z or A-Z) and Blank spaces.
[Hint:(a) When the input string is 'C FOR SWIMMERS, TEST YOUR C PROGRAMMING
STRENGTHS'. Consider the string 'C FOR SWIMMERS' only Because ',' is encountered.
(b) When the input string is 'Y2K PROBLEM'. Consider the character 'Y' only
Because the '2' is encountered.]
/* Counting vowels and consonants in a given line of text */
#include< stdio.h >
#include< string.h >
#include< conio.h >
int main(void)
{ char *str;
int i,vow=0,cons=0;
clrscr();
printf("Enter a string : ");
scanf("%[ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]",str);
for (i = 0; i < strlen(str); i++)
if (str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U'
||
str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u')
vow++;
else if (str[i] != ' ') // Ignore BLANK characters
cons++;
printf("\n\nThe given string is %s",str);
printf("\n\nThe total number of VOWELS in a given string is %d",vow);
printf("\nThe total number of CONSONANTS in a given string is %d",cons);
return(0);
} /* End of Main */
[Q010] W.A.P. in C to perform 4-letter WORD UNSCRAMBLING i.e. List all possible
combinations of 4-letters in a word. Ex: The word 'TEST' can be unscrambled as
TEST,TETS,TSET,TSTE,TTSE,TTES,etc.
/* 4-letter word unscrambling */
#include< stdio.h >
#include< conio.h >
#include< string.h >
int main()
{ int i,j,k,l,sum=6;
char *str;
clrscr();
printf("Enter a 4-letter word or string : ");
scanf("%s",str);
if (strlen(str) == 4)
{ printf("The possible combinations of the given 4-letter word is shown.");
for (i = 0; i < 4; i++)
for (j = 0; j < 4; j++)
if (i != j)
{
for (k = 0; k < 4; k++)
if ((k != i) && (k != j))
{
l = sum - (i + j + k);
printf("\n%c%c%c%c",str[i],str[j],str[k],str[l]);
} }
printf("\nTotal combinations = %d",4*3*2*1);
} else
printf("\nInvalid string. Length of the string must be 4-letters only");
getch();
return(0);
} /* End of Main */
[Q001] Write a program (W.A.P.) in C to clear the screen and print C FOR SWIMMERS
on each line,
forming a diagonal pattern running from upper-left to lower right.[Use suitable
delay]
[Q002] W.A.P. in C to SORT the array of strings based on first 3-LETTERS ONLY.
[Q004] W.A.P. in C to compute the sum of two values using function that takes
two arguments (INTEGERS) and IT SHOULD RETURN the sum WITHOUT USING the RETURN
statement but the return type of function is INTEGER ('int' data type). [NOTE :
DO NOT MAKE USE OF ANY GLOBAL VARIABLES OR POINTER CONCEPT]
[Q006] W.A.P. in C to FIND THE LARGEST of two numbers (integer) WITHOUT using ?:
operator, if, if...else and switch statements. [HINT : USE 'asm' statement]
[Q007] W.A.P. in C to print the following output WITHOUT USING GOTO STATEMENT,
CONDITIONAL STATEMENTS (if, if...else & switch statements) and ANY LOOP
STATEMENTS (for, while & do..while) :
(a)
*****
****
***
**
*
(b)
*
**
***
****
*****
[Q008] W.A.P. in C to REVERSE THE WORDS IN A GIVEN LINE OF TEXT. [HINT : USE
Built-in function 'strtok' in 'string.h' header file]
[Q009] W.A.P. in C to CONVERT THE TEXT within /* ... */ to UPPER-CASE (all
occurrences) from the SPECIFIED C Source Code File and save the file without
altering the remaining text.
[Q010] W.A.P. in C to READ a line of text and WRITE it out BACKWARDS using
RECURSIVE Function.
[Q004] A signed int bitfield 1-bit wide can only hold the values
(a)0 and 1 (b)0 and -1 (c)0, 1 and -1 (d)None of these
[Q007] In DOS environment, what is the maximum combined length of the command-
line arguments passed to main (including the space between adjacent arguments and
the name of the program itself).
(a)80 Characters (b)128 Characters (c)Until RETURN KEY (d)None of these
default: printf("Default2");
} return(0);
}
a)Compile-Time Error(b)Default1Case1 (c)Default2 (d)Default1