Lecture 2 - Primitive Types and Basic Constructs Flashcards

1
Q

What are the primitive data types in C?

A

char: Character type, 8 bits.
short: Short integer, typically 16 bits.
int: Integer, at least 16 bits.
long: Long integer, at least 32 bits.
float: Single precision floating-point, around 6 decimal places.
double: Double precision floating-point, around 10 decimal places.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How are signed and unsigned types different in C?

A

Signed types can represent both positive and negative numbers, while unsigned types can only represent non-negative numbers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is sizeof() in C, and how is it used?

A

sizeof() is an operator that returns the size (in bytes) of a data type or variable. For example, sizeof(int) returns the number of bytes used by an integer.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of the typedef keyword in C?

A

typedef is used to create an alias for a data type, making the code more readable and easier to maintain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How are Boolean values represented in C?

A

In C, Boolean values are represented using integers. Zero (0) is considered false, and any non-zero value is considered true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some common C operators and their functions?

A

+ : Addition
- : Subtraction
* : Multiplication
/ : Division
% : Modulus (remainder)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are the key differences in handling Booleans between C and Java?

A

Java has a dedicated boolean type (true or false), while C uses integers (zero for false, non-zero for true).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of relational operators in C?

A

Relational operators are used to compare two values. Examples include >, <, >=, <=, ==, and !=.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do selection constructs like if and else work in C?

A

The if construct executes a block of code if a condition evaluates to true. The else block is executed if the condition evaluates to false.

if (condition) {
    // true block
} else {
    // false block
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the syntax for a for loop in C?

A
for (initialization; condition; increment/decrement) {
    // loop body
}

Example
~~~
for (int i = 0; i < 10; i++) {
printf(ā€œ%d\nā€, i);
}
~~~

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the switch statement in C?

A

The switch statement is used for multi-way branching based on the value of a variable. It replaces multiple if-else statements and works with int and char types.

switch (variable) {
    case 1:
        // code for case 1
        break;
    case 2:
        // code for case 2
        break;
    default:
        // default case
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between break and continue in loops?

A

break: Exits the loop entirely.
continue: Skips the rest of the current loop iteration and continues with the next one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a do-while loop, and how is it different from a while loop?

A

A do-while loop guarantees that the loop body will execute at least once because the condition is checked after executing the body.

do {
    // code
} while (condition);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How does C handle type casting?

A

C allows both implicit and explicit type casting. Explicit type casting is done using the cast operator:

int x = (int) 5.67;  // x becomes 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are compound assignment operators in C?

A

Compound assignment operators combine an operation with assignment. Examples include +=, -=, *=, /=, and %=.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the purpose of the sizeof() operator?

A

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

17
Q

What is an operator precedence in C?

A

Operator precedence determines the order in which operators are evaluated in an expression. For example, multiplication * has higher precedence than addition +.

18
Q

What is short-circuit evaluation in C?

A

Short-circuit evaluation means that logical expressions (&& and ||) stop evaluating as soon as the result is determined.