0% found this document useful (0 votes)
2 views10 pages

Unit 2 Question Bank Answers

The document provides a comprehensive question bank on C programming, covering topics such as C tokens, constants, operators, variable declarations, and control structures. It includes explanations, syntax, and example programs for various concepts like switch statements, loops, and data types. Additionally, it addresses the differences between related programming constructs and provides practical examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Unit 2 Question Bank Answers

The document provides a comprehensive question bank on C programming, covering topics such as C tokens, constants, operators, variable declarations, and control structures. It includes explanations, syntax, and example programs for various concepts like switch statements, loops, and data types. Additionally, it addresses the differences between related programming constructs and provides practical examples for better understanding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT 2 QUESTION BANK ANSWERS

1. What is a C Token?
A C token is the smallest unit of a C program. It includes:

Keywords (e.g., int, return)


Identifiers (e.g., variable names)
Constants (e.g., 10, 3.14)
Strings (e.g., "Hello")
Operators (e.g., +, -, *, /)
Special symbols (e.g., {, }, ;)

2. Is the Default statement necessary in switch case? What is its significance?


The default statement is not mandatory but is recommended.
If none of the case values match, the default case executes.
It acts as a fallback and improves reliability.
Example:
c
Copy code
switch (x) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
default: printf("Invalid"); // Executes if no case matches
}

3. What is a constant? How to declare a constant in C?


A constant is a fixed value that does not change during program execution.

Ways to declare constants:


Using #define preprocessor directive
c
Copy code
#define PI 3.1415
Using const keyword
c
Copy code
const int x = 10;

4. Write the syntax for the ternary operator


Syntax:

c
Copy code
condition ? expression1 : expression2;
Example:
c
Copy code
int x = (a > b) ? a : b; // Assigns the greater value to x

5. Differentiate between Variable and Constant


Variable Constant
Value can be changed Value remains fixed
Declared normally (int x = 5;) Declared using const or #define
Stored in memory Fixed at compile-time

6. Difference between Implicit and Explicit Conversion


Implicit Conversion Explicit Conversion
Performed automatically by compiler Done manually by programmer
No data loss (e.g., int → float) Possible data loss (e.g., float → int)
Example: int x = 10; float y = x; Example: float x = 10.5; int y = (int)x;

7. Difference between Equality (==) and Assignment (=) Operator


Operator Purpose
= (Assignment) Assigns a value (a = 10;)
== (Equality) Compares two values (if (a == 10))
Example:
c
Copy code
int a = 10; // Assignment
if (a == 10) // Equality check
printf("Equal");

8. Is explicit type conversion necessary? Justify with an example.


Yes, explicit conversion is necessary when precision loss occurs or when converting between
incompatible types.

Example:
c
Copy code
float x = 10.75;
int y = (int)x; // Explicit conversion, y = 10
Without explicit conversion, unwanted behavior may occur.

9. Naming conventions for identifiers in C


Must start with a letter (A-Z or a-z) or underscore _
Can contain letters, digits, and underscores
Cannot be a keyword (e.g., int, return)
Case-sensitive (num ≠ Num)
Should be meaningful (studentAge is better than x)
Valid Examples:
c
Copy code
int student_age;
float _salary;
char firstName[20];
Invalid Examples:
c
Copy code
int 123name; // Cannot start with a digit
float class; // "class" is a keyword

10. Compare pre and post-increment operations with an example


Type Syntax Effect
Pre-increment ++x Increments before using the value
Post-increment x++ Increments after using the value
Example:
c
Copy code
int x = 5;
printf("%d", ++x); // Outputs 6 (Pre-increment)
printf("%d", x++); // Outputs 6, then x becomes 7 (Post-increment)

DESCRPITIVE QUESTIONS AND ANSWERS

Here are brief answers and C program examples for each question:

---

1A. Explain the structure of C.

Structure of a C Program:

A C program follows this basic structure:

#include <stdio.h> // Preprocessor Directive

// Function Definition
int main() {
// Code Statements
printf("Hello, World!");
return 0;
}

Explanation:

#include <stdio.h> – Header file for standard input and output functions.

main() – Main function where program execution starts.

{} – Curly braces mark the beginning and end of a function.

printf() – Output statement.


return 0; – Indicates successful program termination.

---

1B. Write a program to print the maximum of three numbers.

Example Program:

#include <stdio.h>

int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d%d%d", &a, &b, &c);

if(a >= b && a >= c)


printf("Maximum is %d\n", a);
else if(b >= a && b >= c)
printf("Maximum is %d\n", b);
else
printf("Maximum is %d\n", c);

return 0;
}

---

2A. Explain arithmetic, relational, and conditional operators in C with examples.

1. Arithmetic Operators:

+, -, *, /, % Example:

int a = 5, b = 2;
printf("%d", a + b); // Outputs 7

2. Relational Operators:

==, !=, >, <, >=, <= Example:

if(a > b) printf("a is greater");

3. Conditional Operators:

&&, ||, ! Example:

if(a > 0 && b > 0) printf("Both are positive");


---

2B. Write a C Program to print the reverse of a given positive integer.

Example Program:

#include <stdio.h>

int main() {
int n, reverse = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

while(n != 0) {
reverse = reverse * 10 + n % 10;
n /= 10;
}

printf("Reversed number = %d\n", reverse);


return 0;
}

---

3A. What is a variable? What are the rules to declare a variable in C?

Definition:

A variable is a named location in memory used to store data.

Rules:

Must begin with a letter or underscore.

Cannot be a keyword.

Must not contain special characters except _.

Case-sensitive.

Example:

int x = 10;

---

3B. Write a program to print the factorial of a number.


Example Program:

#include <stdio.h>

int main() {
int n, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);

for(int i = 1; i <= n; i++) {


fact *= i;
}

printf("Factorial = %d\n", fact);


return 0;
}

---

4A. Explain the various input and output statements in C with examples.

Input:

scanf() – Reads formatted input. Example:

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

Output:

printf() – Prints formatted output. Example:

printf("%d", x);

---

4B. Write a program to generate a multiplicative table using a while loop.

Example Program:

#include <stdio.h>

int main() {
int n, i = 1;
printf("Enter a number: ");
scanf("%d", &n);
while(i <= 10) {
printf("%d x %d = %d\n", n, i, n * i);
i++;
}

return 0;
}

---

5A. Differentiate between while and do-while loop with an example.

while loop – Condition is checked first.

do-while loop – Executes at least once.

Example:

// while loop
while(condition) {
// code
}

// do-while loop
do {
// code
} while(condition);

---

5B. Write a C Program to check if a number is prime or not.

Example Program:

#include <stdio.h>

int main() {
int n, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);

for(i = 2; i <= n/2; i++) {


if(n % i == 0) {
isPrime = 0;
break;
}
}
if(isPrime && n > 1)
printf("Prime");
else
printf("Not Prime");

return 0;
}

---

6A. Explain ternary operator in C with an example.

Syntax:

condition ? expression1 : expression2;

Example:

int a = 10, b = 20;


int max = (a > b) ? a : b;
printf("Max = %d", max);

---

6B. Write a C program to print all even numbers between 1 and 100.

Example Program:

#include <stdio.h>

int main() {
for(int i = 2; i <= 100; i += 2) {
printf("%d ", i);
}

return 0;
}

---

7A. Explain different data types in C with examples.

Primary Data Types:

int, float, char, double Example:

int x = 10;
float y = 3.5;
char c = 'A';

---

7B. Write a program to count the number of digits.

Example Program:

#include <stdio.h>

int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);

while(n != 0) {
n /= 10;
count++;
}

printf("Digits = %d\n", count);


return 0;
}

---

8A. What is a constant? Explain types with examples.

Integer: const int x = 10;

Float: const float y = 3.14;

Character: const char c = 'A';

---

8B. Explain switch statement to create a calculator.

Example:

switch(operator) {
case '+': result = a + b; break;
case '-': result = a - b; break;
}

---

9A. Explain jumping statements.


break – Exits loop.

continue – Skips current iteration.

return – Exits function.

---

9B. Write a program to check if a number is prime. (Same as 5B)

---

10A. Differentiate between break and continue.

break – Exits loop.

continue – Skips current iteration.

You might also like