NEW
Font size
WorksheetsC Programming Fundamentals
Total questions: 24
Worksheet time: 12mins
What are the basic data types in C?
int, float, double, char, void
boolean
array
string
How do you declare an integer variable in C?
int myVariable;
var myVariable;
integer myVariable;
float myVariable;
What is the purpose of the 'if' statement in C?
To declare variables in C.
To execute code conditionally based on a boolean expression.
To create loops in C.
To define a function in C.
Explain the difference between '==' and '=' in C.
'=' is used for comparison; '==' is for assignment.
'=' assigns a value; '==' checks for equality.
'=' checks for equality; '==' assigns a value.
Both '=' and '==' are used for assignment.
What is a for loop and how is it structured in C?
A for loop in C is structured as: for(initialization; condition; increment/decrement) { // code }
for(condition; initialization; increment/decrement) { // code }
for(initialization; increment/decrement; condition) { // code }
for(initialization: condition: increment/decrement) { // code }
Write a simple for loop that prints numbers 1 to 5.
for i in range(1, 4): print(i)
for i in range(1, 6): print(i)
for i in range(1, 7): print(i)
for i in range(5): print(i)
What is the syntax for a while loop in C?
while(condition) [ code to be executed ]
while(condition) { /* code to be executed */ }
while { condition; }
do { code to be executed } while(condition);
Provide an example of a while loop that counts down from 10 to 1.
int count = 10; while (count > 0) { System.out.println(count); count--; }
int count = 1; while (count < 10) { System.out.println(count); count++; }
for (int count = 10; count > 0; count++) { System.out.println(count); }
while (count < 10) { System.out.println(count); count--; }
What is the purpose of the 'else' statement in C?
The 'else' statement is used to terminate a program.
The 'else' statement provides an alternative action when the 'if' condition is false.
The 'else' statement is used to declare variables.
The 'else' statement is a loop control structure.
How do nested if-else statements work in C?
Nested if-else statements automatically execute all conditions regardless of the outcomes.
Nested if-else statements are not allowed in C programming.
Nested if-else statements can only evaluate one condition at a time.
Nested if-else statements work by evaluating conditions in a hierarchical manner, allowing for multiple layers of decision-making.
What is type conversion in C?
Type conversion in C is only done implicitly.
Type conversion in C refers to changing the variable's name.
Type conversion in C is the process of converting a variable from one data type to another, either implicitly or explicitly.
Type conversion in C is the process of changing a variable's scope.
Explain implicit type conversion with an example.
Example: In JavaScript, `5 + 5` results in `10` because both are numbers.
Example: In JavaScript, `5 + '5'` results in `'55'` because the number 5 is implicitly converted to a string.
Example: In JavaScript, `5 - '5'` results in `0` because the string is treated as zero.
Example: In JavaScript, `5 * '5'` results in `5` because the string is ignored.
What is explicit type conversion and how is it done?
Implicit type conversion is done automatically by the compiler.
Explicit type conversion is the process of converting a variable from one data type to another using specific functions or methods.
Explicit type conversion is only possible in certain programming languages.
Type conversion is the same as type declaration.
Write a code snippet that demonstrates type casting in C.
#include
float fnum = num; // Incorrect type assignment
printf(fnum); // Missing format specifier
int fnum = (int)10.5; // Invalid casting
What will be the output of the following code: int a = 5; float b = a; printf('%f', b);?
5.000
5.0
5.00000
5.000000
How do you use logical operators in if statements?
Use logical operators like '&&' for AND, '||' for OR, and '!' for NOT in if statements to evaluate multiple conditions.
Use 'AND' and 'OR' keywords instead of symbols in if statements.
Use '==' for AND, '!=' for OR, and '??' for NOT in if statements.
Logical operators are not used in if statements.
What is the difference between '&&' and '||' operators?
The '&&' operator requires both conditions to be true, while the '||' operator requires at least one condition to be true.
The '&&' operator can only be used with numbers, while the '||' operator can only be used with strings.
The '&&' operator is used for bitwise operations, while the '||' operator is used for logical comparisons.
The '&&' operator requires at least one condition to be true, while the '||' operator requires both conditions to be true.
Write a code snippet that uses a nested if-else statement.
if (number > 0) { if (number % 2 == 0) { console.log('Positive Even'); } else { console.log('Positive Odd'); } } else if (number < 0) { console.log('Negative'); } else { console.log('Zero'); }
if (number == 0) { console.log('Zero'); }
if (number > 10) { console.log('Greater than 10'); }
if (number < 0) { console.log('Positive'); }
How can you use a switch statement as an alternative to if-else?
A switch statement is used to loop through an array of values.
Use a switch statement to compare two boolean values directly.
Use a switch statement to evaluate a variable against multiple cases, executing the corresponding block of code for the matching case.
Switch statements can only handle numeric values.
What is the output of the following code: int x = 10; if(x > 5) { printf('Greater'); } else { printf('Smaller'); }?
None
Greater
Lesser
Equal
What is the output of the following code: char c = 'A'; printf('%c', c + 1);?
66
65
A
B
Which of the following correctly initializes an array of integers in C?
int arr[5]; arr = {1, 2, 3, 4, 5};
int arr[5] = {1, 2, 3, 4, 5};
int arr = {1, 2, 3, 4, 5};
int arr[5] = new int[5];
What is the purpose of the 'break' statement in C?
The 'break' statement is used to exit a loop or switch statement prematurely.
The 'break' statement is used to pause the execution of a program.
The 'break' statement is used to create an infinite loop.
The 'break' statement is used to declare a variable.
What is the output of the following code: int a = 5; int b = 10; printf('%d', a + b);?
None
5 + 10
510
15
