0% found this document useful (0 votes)
35 views

Week 1 Questions Sr. No. Options Answer & Explanation

The document contains 11 programming questions related to C language concepts like loops, operators, precedence, etc. along with sample inputs, outputs and explanations. It tests the understanding of basic C programming constructs through multiple choice and coding questions on topics like loops, operators, precedence, conditional statements, functions and more. Suggested solutions are provided for the coding questions to help learners self-evaluate and improve their programming skills.

Uploaded by

Virat Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Week 1 Questions Sr. No. Options Answer & Explanation

The document contains 11 programming questions related to C language concepts like loops, operators, precedence, etc. along with sample inputs, outputs and explanations. It tests the understanding of basic C programming constructs through multiple choice and coding questions on topics like loops, operators, precedence, conditional statements, functions and more. Suggested solutions are provided for the coding questions to help learners self-evaluate and improve their programming skills.

Uploaded by

Virat Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Week 1 Questions

Sr. Question Options Answer & Explanation


no.

1 Find the output: A. 10 Answer: D


#include<stdio.h> B. 20
int main() C. 21 Explanation:
D. 11 x++ increments the value to 11.
{
So printf statement uses x=11.
int x=10; int y;
{
y=x++;
}
printf("%d",x);
}

2 Find the output: A. 97 Answer: A


#include<stdio.h> B. 98
int main() C. 99 Explanation:
{ D. ‘a’ The ASCII value of ‘a’ is 97
char c='a'; which gets switched while
switch(c){ switching the char c
case 97:
printf("97");
break;
case 98:
printf("98");
break;
case 99:
printf("99");
break;
default:
printf("default");

}
}
3 Find the output: A. 1,10 Answer: C
#include<stdio.h> B. 9,11
int main() C. 9,19 Explanation: The expression
{ D. 1,19 b*c+d-a gets evaluated first,
int x=10; which then sets the value of a
int a=1,b=2,c=3,d=4; to 9. This value is used for
x+=a=b*c+d-a; incrementing x.
printf("%d,%d",a,x);
}

4 Find the output: A. YES Answer: B


#include<stdio.h> B. NO
int main() C. Error Explanation:
{ int b = 1.1 assigns 1 to b.
float a=1.1; Comparing a==b compares 1
int b=1.1; with 1.1 which is FALSE.
if(a==b)
printf("YES");
else
printf("NO");
}

5 Find the output: 11 Answer: B


#include<stdio.h>
int main() Explanation:
{ The semicolon after the for loop
int i,x=10; terminates it and the brackets
for(i=0;i<2;i++); are treated as a single block,
{ thus x++ is executed only once.
x++;
}
printf("%d",x);
}
6 Find the output: 13 Answer: A
#include<stdio.h>
int main() Explanation:
{ Condition is evaluated to true
int i=-10; for any non-zero value. So, the
if(i){ first and last IF conditions are
printf("1"); evaluated as TRUE.
}
i=0;
if(i){
printf("2");
}
i=5;
if(i){
printf("3");
}
}

7 Find the output: A. 10 Answer: C


#include<stdio.h> B. 11
int main() C. 12 Explanation:
{ D. 13 The do loop executes once and
int x=10; E. The goes to the check. In the check,
do{ program runs for there is one more increment.
x++; more than 10 So the final answer is 12.
}while(x++>12); iterations.
printf("%d",x);
}

9 Find the output: A. 2 Answer: C


void main(){ B. 3 Explanation:
int a=22; C. 1 Right shift 4 times of 22 is
a=a>>4; D. 5 22/16 = 1 (integer division)

printf("%d",a);
}

10 Find the output: A. 7 Answer: A


void main(){ B. 8 Explanation:
int a,b; C. 6 , can also be used as an
a=3,1; D. Error operator and it has the least
precedence. So in the first
b=(5,4);
statement assignment is done
printf("%d",a+b);
first. In the second, assignment
}
is done later.
11 Which of the following condition A. a==10 Answer : C
checks will print 10 twice? B. a <= 10 Explanation : printf returns
C. printf("\n number of characters printed.
void main(){ %d",a) So the first 10 can be printed
int a=10; D. None of the using the condition. Since the
if(_______) above statement prints more than 0
printf("\n%d",a); characters, the IF condition is
else true and hence the IF part will
printf("\n%d %d",a,a); print one more 10.
}
12 #include<stdio.h> 9 Answer : B
main ( ) Answer :
{ 5 % (5 − 5/2 ) ∗ ( 5 −3)+5

int a=5; = 5 % ( 5 − 2 ) ∗ (5 − 3 ) +

int b = a % ( a−a /2 ) ∗ ( a 5

−3)+a; =5%3∗2+5
=2∗2+5
printf(”%d”,b);
=9
return 0 ;
}
14 #include<stdio.h> a. 1 a
b. 2 The precedence order is *, +,
int main(){ c. 4 >> and lastly +=
int i=1; d. 7 Therefore Ans = 1 + ((1
i += i*i>>2+3; *1)>>(2+3)) = 1 + ((1)>>(5)) =
printf("%d",i); 1+0 = 1
return 0;
}
Give the result of compiling and
running the above program?
15 #include<stdio.h> a. 15 c.
b. 24
int main(){ c. 33 The precedence order is *, +,
int i=1; d. 34 >> and lastly +=
i += i*i<<2+3; Therefore Ans = 1 + ((1
printf("%d",i); *1)<<(2+3)) = 1 + ((1)<<(5)) =
return 0; 1+32 = 33
}
Give the result of compiling and
running the above program?

16 char a,b,c; a. AABbCc c.


a = 'b'; b. AABbCc b = c => b = 'A'
b = 'c'; c. AABACA c = b => c = 'A'
c = 'A'; d. AcBbCA a = c => a = 'A'
b=c;c=b; c = a => c = 'A'
a=c;c=a;
printf("A%cB%cC%c",a,b,c);
What will be the output printed
by the printf statement?

Programming Question #1

Write a program to find the number of perfect squares between given two numbers A and B
(both inclusive). A number is called a perfect square if it can be written as x*x for some integer x.

Constraints:
Both A and B are positive. They both are less than 100,000.

Input: Two numbers A and B separated by a space


Output: Count of the number of perfect squares
Example 1:
Input: 3 10
Output: 2

Example 2:
Input: 16 70
Output: 5

Public Test cases:

Number Input Output


1. 3 10 2
2. 16 70 5
3. 11 99 6

Private Test cases:

Number Input Output


1. 1 100 10
2. 17 124 7
3. 12 1
4. 46 47 0
5. 10000 20000 42

Solution:

#include<stdio.h>

int main(){
int x,y,i,a;
int count=0,flag=0;

scanf("%d %d",&x,&y);

for(i=x;i<=y;i++){
for(a = 0; a <= i; a++)
{
if (i == a * a)
{
flag=1;
}
}
if(flag==1)
count++;
flag=0;
}
printf("%d",count);
return 1;
}

Programming Question #2

Write a program to find whether a given number (say x) is a “perfect number” or not.

Definition of Perfect number:


A perfect number is a positive integer that is equal to the sum of its proper positive divisors.

Explanation:
Take number 6.
Proper positive divisors of 6 is 1,2,3 and their sum is 1+2+3=6.
So, 6 is a perfect number.

Constraint:
x>=1

Input: A single number x


Output:
yes if given number x is a perfect number
no if given number x is not a perfect number

Example 1:
Input: 6
Output: yes

Example 2:
Input: 7
Output: no

Public Test cases:

Number Input Output


1. 6 yes
2. 10 no
3. 28 yes

Private Test cases:

Number Input Output


1. 1 no
2. 496 yes
3. 8128 yes
4. 1000 no
5. 4042 no
6. 33550336 yes

Solution:

#include<stdio.h>

int main(){
int x,i,sum=0;

scanf("%d",&x);

for(i=1;i<x;i++){
if(x%i==0){
sum+=i;
}
}
if(sum==x){
printf("yes");
}else{
printf("no");
}
return 1;
}

Programming Qn #3
Write a C program that takes a positive number N and produces an output that is the product of
its digits.

Explanation:
Take number 657.
Answer expected : 6 * 5 * 7 = 210
Constraint:
1<=N<=999999
Input: A single number
Output:
The value
Example 1:
Input: 657
Output: 210
Example 2:
Input: 7
Output: 7

Public Test cases:


Number Input Output
1. 657 210
2. 41 4
3. 931 27
Private Test cases:
Number Input Output
1. 1 1
2. 201 0
3. 1234 24
4. 5555 625
5. 10156 0
6. 999999 531441
7. 124356 720
8. 111111 1

Programming Qn #4

Given three points (x1, y1), (x2, y2) and (x3, y3) , write a program to check if all the
three points fall on one straight line.

INPUT:
Six integers x1, y1, x2, y2, x3, y3 separated by whitespace.

OUTPUT:
Print “Yes” if all the points fall on straight line, “No” otherwise.

CONSTRAINTS:
-1000 <= x1, y1, x2, y2, x3, y3 <= 1000

Public Test cases:


Number Input Output
1. 100030 Yes

2. -2 0 -2 1 -2 2 Yes
3. -62 14 -18 -23 -6 23 No

You might also like