21 Lecture
CS304
Midterm & Final Term Short Notes
BEHAVIOR OF ++ AND -- FOR PRE-DEFINED TYPES
The behavior of the ++ and -- operators for pre-defined types depends on whether they are used in a prefix or postfix context. When used as a prefix, they increment or decrement the value of the operand by 1 and return the new value. When used a
Important Mcq's
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is the result of the following code snippet?
css
Copy code
int a = 5;
int b = a++;
a) a = 5, b = 6
b) a = 6, b = 5
c) a = 5, b = 5
d) a = 6, b = 6
Answer: a) a = 5, b = 6
What is the result of the following code snippet?
css
Copy code
int a = 5;
int b = ++a;
a) a = 5, b = 6
b) a = 6, b = 5
c) a = 5, b = 5
d) a = 6, b = 6
Answer: b) a = 6, b = 6
What is the result of the following code snippet?
css
Copy code
float a = 5.0;
float b = a++;
a) a = 5.0, b = 6.0
b) a = 6.0, b = 5.0
c) a = 5.0, b = 5.0
d) a = 6.0, b = 6.0
Answer: a) a = 6.0, b = 5.0
What is the result of the following code snippet?
css
Copy code
float a = 5.0;
float b = ++a;
a) a = 5.0, b = 6.0
b) a = 6.0, b = 5.0
c) a = 5.0, b = 5.0
d) a = 6.0, b = 6.0
Answer: b) a = 6.0, b = 6.0
What is the result of the following code snippet?
css
Copy code
char a = 'a';
char b = a++;
a) a = 'a', b = 'b'
b) a = 'b', b = 'a'
c) a = 'a', b = 'a'
d) a = 'b', b = 'b'
Answer: a) a = 'b', b = 'a'
What is the result of the following code snippet?
css
Copy code
char a = 'a';
char b = ++a;
a) a = 'a', b = 'b'
b) a = 'b', b = 'a'
c) a = 'a', b = 'a'
d) a = 'b', b = 'b'
Answer: b) a = 'b', b = 'b'
What is the result of the following code snippet?
css
Copy code
int a = 5;
int b = a-- + 3;
a) a = 5, b = 8
b) a = 4, b = 7
c) a = 5, b = 7
d) a = 4, b = 8
Answer: c) a = 4, b = 8
What is the result of the following code snippet?
css
Copy code
int a = 5;
int b = --a + 3;
a) a = 5, b = 7
b) a = 4, b = 7
c) a = 5, b = 6
d) a = 4, b = 6
Answer: b) a = 4, b = 6
Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included
Download PDF
What is the difference between prefix and postfix increment/decrement operators? Answer: Prefix increment/decrement operators modify the value of the operand before using it, while postfix increment/decrement operators modify the value of the operand after using it. What is the behavior of the increment/decrement operators for integer types? Answer: The increment/decrement operators for integer types increment or decrement the value of the operand by 1. What is the behavior of the increment/decrement operators for floating-point types? Answer: The behavior of the increment/decrement operators for floating-point types can lead to rounding errors. What is the behavior of the increment/decrement operators for pointer types? Answer: The behavior of the increment/decrement operators for pointer types depends on the underlying architecture, but generally, they move the pointer by the size of the pointed-to type. What happens when we use the increment/decrement operators on an unsigned integer type and the result would be negative? Answer: The behavior is undefined, and it's essential to use these operators with care to avoid unintended side effects or undefined behavior. What is the result of the following code snippet? css Copy code int a = 5; int b = ++a + a++ + ++a; Answer: The result is undefined because the order of evaluation of the expressions is unspecified. What is the result of the following code snippet? css Copy code int a = 5; int b = a++ * ++a; Answer: The result is undefined because the order of evaluation of the expressions is unspecified. What is the result of the following code snippet? css Copy code int a = 5; int b = ++a * a++; Answer: The result is undefined because the order of evaluation of the expressions is unspecified. What happens when we use the increment/decrement operators on a constant variable? Answer: It's not allowed to use the increment/decrement operators on a constant variable, and it results in a compilation error. Can we use the increment/decrement operators on Boolean types? Answer: No, we cannot use the increment/decrement operators on Boolean types because they are not arithmetic types.