Breaking News

BCA Semester 1: Programming Using C - Important Questions

Programming Using C 


Important Questions asked in BCA Semester I (IPU)


Q1: Explain the difference between entry-controlled loop and exit controlled loop

Answer: Depending, on the position of the Control Statement in the loop, a control structure may be classified either as entry-controlled loop or exit-controlled loop.

1)  In the entry-contolled loop, the conditions are listed before the start of the loop execution.
Example: For loop and while loop.

2) In the exit-controlled loop , the test-conditions are performed at the end of the loop and therefore the body is executed unconditionally for the first time.
Example: Do-while Loop.

At least one time execution is done compulsory.

Entry Controlled Loop

IP University BCA Sem 1: Entry Controlled Loop

Exit Controlled Loop

IP University BCA Sem1: Exit Controlled Loop

Q2: What is the difference between Post-fix and Pre-fix increment or decrement operator


Answer: The prefix form first performs the increment or decrement operation and then returns the value of the increment/decrement operation.

The postfix form first returns the current value of the expression and performs the increment or decrement operaton on that value.

For Example:

 int a = 5;
printf("Value of a = %d",a);
printf("\nValue of a++: %d",a++);
printf("\nValue of ++a: %d",++a);

printf("\nValue of a: %d",a);
printf("\nValue of a--: %d",a--);
printf("\nValue of --a: %d",--a);

OUTPUT:
Value of a: 5
Value of a++: 5
Value of ++a: 7
Value of a: 7
Value of a--: 7
Value of --a: 5


Above example clearly explains us the difference between postfix and prefix increment/decrement operator.

Q 3: What is the difference between a++ and a+=1?

Answer: a++ is a post-increment operator whereas a+=1 is a shorthand operator. Both are doing the same thing, that is they are adding 1 to the value of 'a' and storing it inside the value of 'a' variable.
These statements are same as a = a + 1;

Q 4: Write the difference between:
a) | and ||
b) & and &&


Answer: | and || are OR operators only.
But | is Bitwise OR operator and || is Logical OR operator.
Similarly, & and && are AND operators only.
But & is Bitwise AND operator and && is Logical AND operator.

Q 5: State the difference between variable and identifier.

Answer:
Identifier refers to the name of a variable, function, arrays, pointers, structures etc. These are user defined names and consists of a sequence of letters and digits. Underscore(_) is also permitted.
Examples: num, var1,rno[10],*ptr,sum etc.

Variable
A variable, a type of identifier, is a data name that may be used to store data value. Unlike constants that remain unchanged during the execution of a program, a variable may take different values at different time during execution.
Examples: a,b,c,_perc,roll_number,count etc. Note: Rules for declaring a variable and an identifier are same.

Q 6: What do you mean by symbolic constant? How is it defined? Explain with a suitable example.

Answer: A Symbolic Constant is name that substitute for a sequence of character that cannot be changed. The character may represent a numeric constant, a character constant or a string. When the program is compiled, each occurrence of a symbolic constant is replaced by its corresponding character sequence. They are usually defined at the beginning of the program. The symbolic constants may then appear later in the program in place of the numeric constants, character constants,etc. that the symbolic constants represent.
For example:
A C program consists of the following symbolic constant definitions.


#define PI 3.141593
#define TRUE 1
#define FALSE 0

#define PI 3.141593 defines a symbolic constant PI whose value is 3.141593. When the program is pre-processed, all occurrences of the symbolic constant PI are replaced with the replacement text 3.141593.
Note that the preprocessor statements begin with a # symbol, and are not ended by a semicolon. By convention, preprocessor constants are written in UPPERCASE.
Example:

#include 
#include

#define TRUE 1
#define FALSE 0
#define PI 3.141593

void main()
{
float a;
float b;
float c;
float d = PI;
clrscr();
if(TRUE)
{
a = 100;
b = a * 10;
c = b - a;
}
printf("\na = %f \nb = %f \nc = %f\nd = %f",a,b,c,d);
getch();
}


Output:
a = 100.0
b = 1000.0
c = 900.0
d = 3.141593

Q7: Write a program in C
  1. To generate
    1 (Floydd's Triangle)
    2 3
    4 5 6
  2. To find out the reverse of a number, accepted by a user
  3. To find xn
  4. To calculate the factorial of the number accepted by the user.

Answer:
i) //Floydd's Triangle
#include
#include

int main()
{
int i,j, n = 1;
clrscr();
for(i = 1; i <=3; i++)
{
for(j = 1; j <= i; j++)
printf("%d",n++);
printf("\n");
}
getch();
return 0;
}

ii) //Reverse of any number
#include
#include

int main()
{
int n;
clrscr();
printf("Enter any number: ");
scanf("%d",&n);
printf("\nAfter reversing...");
printf("\nNumber is: ");
while(n>0)
{
printf("%d",n%10);
n = n/10;
}
getch();
return 0;
}

iii)//to find xn

#include
#include

int main()
{
int i,x,n,y;
clrscr();
printf("Enter the value of x: ");
scanf("%d",&x);
printf("Enter the value of n(which is raised to the power of x): ");
scanf("%d",&n);
y = x;
if(n == 0)
{
printf("xn is 1");
getch();
exit(0);
}

for(i = 1; i < n; i++)
x = x*y;
printf("\nxn is %d", x);
getch();
return 0;
}

iv) //Factorial of a number

#include
#include

int main()
{
int n,x = 1,i;
clrscr();
printf("Enter a number whose factorial is to be calculated: ");
scanf("%d",&n);
if((n==0)||(n == 1))
{
printf("Factorial of the number is 1");
getch();
exit(0);
}
for(i = n; i > 1; i--)
x *= i;
printf("Factorial is %d is %d",n,x);
getch();
return 0;
}

Q8: Explain the primitive data types with ranges available in C language.

Answer:

Primary Or Fundamental Or Primitive Data Types

Integer Data Types (int)


Integer data type is used to declare a variable that can store numbers(integers) without a decimal. The keyword used to declare a variable of integer type is "int". Thus, to declare integer data type following syntax should be followed:
 int variable_name;

Range: -32,768 to 32,767
It requires 2 bytes to store in memory.

Character Data Type (char)


Character data type declares a variable that can store a character constant. Thus, the variables declared as char data type can only store one single character.
Syntax:
 char variable_name 

Range: -128 to 127
It requires ony one(1) byte to store.

Float Data Type (float)


Float data type declares a variable that can store numbers containing a decimal point number.
Syntax:
 float variable_name 

Range: -3.4E38 to 3.4E38
It requires 4 bytes to store.

Void Data Type (void)


Unlike other primitive data types in C, void data type does not create any variable but returns an empty set of values. Thus, we can say that it stores null.
Syntax:
 void variable_name