Showing posts with label Basics. Show all posts
Showing posts with label Basics. Show all posts

Sunday, August 24, 2025

Viva questions based on C Decision Making statements with Answers

Viva Questions on C Decision Making Statements with Answers

(if, if-else, nested if, switch, conditional operator)

1. What is a decision-making statement in C?

Answer:

A decision-making statement allows the program to take different actions based on conditions. Examples are if, if-else, nested if, switch, and the conditional (ternary) operator ?:.

2. What is the difference between if and if-else statements?

Answer:

if executes a block of code only if the condition is true.
if-else provides an alternative block of code to execute when the condition is false.

3. Can we use multiple if-else statements in C?

Answer:

Yes, we can use multiple if-else if constructs, also known as ladder if-else, to check multiple conditions sequentially.

4. What is a nested if statement?

Answer:

A nested if means writing an if statement inside another if or else block. It is useful when multiple levels of decision-making are required.

5. How does a switch statement work in C?

Answer:

A switch evaluates an expression and compares it with multiple case labels. When a match is found, the corresponding block is executed until a break statement is encountered.

6. What happens if we don’t use break in a switch case?

Answer:

Without break, the program executes the matched case and then continues executing subsequent cases (fall-through) until it encounters a break or the end of the switch block.

7. Can a switch statement work with floating-point values?

Answer:

No, in C, a switch statement works only with integer and character values, not with float or double.

8. What is the difference between if-else and switch?

Answer:

if-else can handle complex conditions (logical expressions, ranges, multiple conditions).
switch is limited to checking equality of a single expression against constant values but is often faster and more readable.

9. What is the conditional (ternary) operator in C?

Answer:

The conditional operator ?: is a shorthand for if-else.

Example:

int max = (a > b) ? a : b;

This assigns a to max if a > b, otherwise assigns b.

10. Is it possible to use a switch inside another switch?

Answer:

Yes, we can have nested switch statements, but it is not commonly used because it reduces readability.

11. What is the difference between = and == in decision-making?

Answer:

= is the assignment operator (assigns value).
== is the equality operator (compares two values).
Using = instead of == inside conditions is a common error.

12. Can if statements be written without curly braces {}?

Answer:

Yes, if there is only one statement inside the if or else block, curly braces are optional. But for multiple statements, curly braces are required.


 < Back to Question Bank >

 

Saturday, August 23, 2025

C Programming Viva Questions on Loops with Model Answers

Viva Questions on Loops with Answers


1. What is a loop in C programming? Why do we use it?

Answer:
A loop is a control structure in C that repeatedly executes a block of code until a specified condition is true. We use loops to avoid writing repetitive code and to perform iterative tasks efficiently.

2. Explain the difference between while, do…while, and for loops in C.

Answer:
  • while loop: Condition is checked first, then the body executes. (Entry-controlled loop)
  • do…while loop: Body executes first, then condition is checked. (Exit-controlled loop, runs at least once)
  • for loop: Used when initialization, condition, and increment/decrement are all in one line. Best for fixed iterations.
3. What happens if the loop condition is never satisfied in a while loop?

Answer:

If the condition is false at the beginning, the while loop body will never execute even once.

4. Can a for loop run infinitely? Give an example.

Answer:

Yes. If the loop condition is always true or missing, the loop runs infinitely.

Example:

for(;;) {
   printf("Infinite loop\n");
}

5. What is the difference between break and continue statements inside loops?

Answer:

•   break: Immediately exits the loop and transfers control outside.
•   continue: Skips the current iteration and jumps to the next iteration of the loop.

6. What is an infinite loop? Write a simple example in C.

Answer:

An infinite loop is a loop that never terminates because the condition always remains true.

Example:

while(1) {
   printf("Hello\n");
}

7. How is the initialization, condition, and increment part of a for loop optional? Give an example.

Answer:

All three parts in a for loop are optional. If omitted, they must be handled inside the loop body.

Example:

int i = 1;
for(; ; ) {
   if(i > 5) break;
   printf("%d ", i);
   i++;
}

8. What is the output of the following code?

int i = 1;
while(i <= 5){
    printf("%d ", i);
    i++;
}
Answer:
Output:
1 2 3 4 5

9. Write a C program to print numbers from 1 to 10 using a do…while loop.

Answer:

#include <stdio.h>
int main() {
    int i = 1;
    do {
        printf("%d ", i);
        i++;
    } while(i <= 10);
    return 0;
}

10. Explain nested loops with an example (e.g., printing a multiplication table).

Answer:

A nested loop is a loop inside another loop.
  
Example of Multiplication table:

for(int i = 1; i <= 3; i++) {
    for(int j = 1; j <= 3; j++) {
        printf("%d ", i * j);
    }
    printf("\n");
}
Output:
1 2 3  
2 4 6  
3 6 9

 < Back to Question Bank >


Sunday, April 20, 2025

C Programming Viva Questions Data Types

Viva Questions based on C Programming Data Types

 

1. What are data types in C?

Answer: It defines the type of data that we can store in a variable. They tell the compiler how much space to allocate and how to interpret the value.

 

2. What are the different types of data types in C?

Answer: C has three primary data types:

•   Basic data types: int, float, char, double

•   Derived data types: array, pointer, structure, union

•   Enumeration and void types: enum, void

 

3. What is the size of int, float, double, and char in C?

Answer: Sizes may vary by system, but typically:

•   int: 4 bytes

•   float: 4 bytes

•   double: 8 bytes

•   char: 1 byte

 

4. What is the difference between float and double?

Answer:

•   float is a single-precision (32-bit) floating point.

•   double is a double-precision (64-bit) floating point. double is more accurate and can store larger values.

 

5. What is the use of the void data type?

Answer: void means “no type.” It is used:

•   In functions that return nothing: void functionName()

•   For generic pointers: void *ptr

 

6. What is the difference between signed and unsigned data types?

Answer:

•   Signed data types can store both positive and negative values.

•   Unsigned data types can only store positive values, which allows a larger positive range.

Example:

signed int ranges from2,147,483,648 to 2,147,483,647

unsigned int ranges from 0 to 4,294,967,295

 

7. What is the use of sizeof operator?

Answer: The sizeof operator returns the size (in bytes) of a data type or variable.

Example: sizeof(int) gives 4 on most systems.

 

8. What are type modifiers in C?

Answer: Type modifiers are keywords that alter the meaning of the base data types.

They are: signed, unsigned, long, and short.

Example: unsigned long int increases the range for positive integers.

 

9. Can we use char for storing small integers?

Answer: Yes. A char is 1 byte, so it can store small integer values (typically –128 to 127 for signed char).

 

10. What happens if we assign a float value to an int variable?

Answer: The decimal part is truncated, not rounded.

Example:

float x = 5.9;

int y = x; // y becomes 5

 

 

Monday, June 12, 2023

Top 10 C Programming Topics for Beginners

Followings are the top 10 topics for the C programming beginners. These topics are essential for understanding the C programming language and for writing effective C programs:

Top 10 Topics for C Programming Beginners

  1. Data types
  2. Variables
  3. Input / Output (I/O) Operations
  4. Operators
  5. Control flow statements
  6. Functions
  7. Arrays
  8. Strings
  9. Pointers
  10. File handling

Here is a brief overview of each topic:

  • Data types: Data types are the building blocks of C programs. They define the type of data that can be stored in a variable.
  • Variables: Variables are used to store data. They are given names and can be used to store values of different data types.
  • Input / Output (I/O) Operations: I/O operations are used to manage data input from the user or a File. printf() and scanf() are two popular I/O functions.
  • Operators: Operators are used to perform operations on data. They can be used to perform arithmetic operations, logical operations, and string operations.
  • Control flow statements: Control flow statements are used to control the flow of execution of a C program. They can be used to make decisions, loops, and jumps.
  • Functions: Functions are blocks of code that are reusable. They can be called from other parts of a C program.
  • Arrays: Arrays are variables that can store multiple values of the same data type.
  • Strings: Strings are arrays of characters. They are used to store text data.
  • Pointers: Pointers are variables that store the address of another variable. They can be used to access the value of another variable indirectly.
  • File handling: File handling is used to read and write data to files. It is a powerful tool that can be used to store and retrieve data from files.

These are just a few of the many topics that every C programming beginners should learn. If you are interested in learning C programming, we recommend that you find a good book or online Self Study Tutorials that covers these topics in detail.




Wednesday, February 15, 2023

Comparison between Python, C and Java

This article provides comparison between three most popular computer programming languages: Python, C and Java.

Python, C and Java


Python, C, and Java are three different programming languages. Each language has its own strengths and weaknesses. Following is a short comparison of these three computer programming languages:

Python


Python is a high-level, interpreted language that is known for its simplicity, readability, and versatility. It has a vast standard library, and it is often used for scripting, web development, data analysis, machine learning, and artificial intelligence. It is dynamically typed, which means that variables don't need to be explicitly declared, and it has a simple syntax that makes it easy to learn and use.

  • Pros: Easy to learn, concise and readable syntax, large standard library, dynamic typing, strong support for scientific computing and data analysis, good for scripting and automation tasks.
  • Cons: Slower compared to other compiled languages, not the best option for low-level programming, doesn't offer as much control over memory management, and not ideal for developing large-scale applications.

C Language

C is a low-level language that is used for system programming, embedded systems, and other tasks that require direct access to hardware. It is a compiled language, which means that it is translated into machine code by a compiler before being executed. C is known for its efficiency, control over memory management, and low-level access to system resources.

  • Pros: Fast and efficient, direct access to system resources, control over memory management, widely used in system programming and embedded systems.
  • Cons: Complex syntax, difficult to learn, no built-in support for object-oriented programming, no automatic garbage collection, prone to memory leaks and buffer overflows.

Java


Java is a high-level language that is known for its portability, security, and object-oriented programming features. It is compiled into bytecode, which is then interpreted by the Java Virtual Machine (JVM). Java is used for a wide range of applications, including web development, mobile app development, and enterprise software.

  • Pros: Platform independence, strong support for object-oriented programming, automatic memory management, rich standard library, good for developing large-scale applications.
  • Cons: Slower compared to other compiled languages, requires the JVM to be installed on the system, more verbose syntax than Python, and requires more memory compared to other languages.

Summary:

In summary, each language has its own strengths and weaknesses, and the choice of language depends on the specific requirements of the project. Python is often used for scripting and data analysis, C is used for system programming and embedded systems, and Java is used for enterprise software and mobile app development.


Sunday, January 23, 2022

Check whether a character is an alphabet, digit or special character

This program is based on C Programming Decision making concept (use of if..else statement).

Problem definition: Write a C program to check whether a given character is an alphabet, digit or special character.

#include <stdio.h>
int main()  
{  
    char chr;  

    printf("Enter a character: ");  
    scanf("%c", &chr);  
  
    /* logic to check whether it is an alphabet */  
    if((chr >= 'a' && chr<='z') || (chr>='A' && chr<='Z'))  
    {  
        printf("It is an alphabet.\n");
    }  
    else if(chr>='0' && chr<='9') /* to check digit */  
    {  
        printf("It is a digit.\n");  
    }  
    else /* else special character */
    {  
        printf("It is a special character.\n");  
    }  
}

Sample output 1

Enter a character: A
It is an alphabet.

Sample output 2

Enter a character: 5
It is a digit.

Sample output 3
Enter a character: @
It is a special character.