Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views14 pages

Syntax&Output

The document contains a series of C and C++ programming questions and answers, covering topics such as variable naming, memory allocation, output of code snippets, and syntax rules. Each question is followed by multiple-choice options and the correct answer is provided. The content serves as a quiz or study guide for programming concepts in C and C++.

Uploaded by

Antonuose Gerges
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)
10 views14 pages

Syntax&Output

The document contains a series of C and C++ programming questions and answers, covering topics such as variable naming, memory allocation, output of code snippets, and syntax rules. Each question is followed by multiple-choice options and the correct answer is provided. The content serves as a quiz or study guide for programming concepts in C and C++.

Uploaded by

Antonuose Gerges
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/ 14

#include <stdio.

h>
int main() {
printf("%d", sizeof('A'));
return 0;
}

Options:
A) 1
B) 2
C) 4
D) Depends on the compiler

Answer: D) Depends on the compiler


(In C, 'A' is treated as an int, so sizeof('A') is usually 4. In C++, it is 1.)

2. Which of the following is not a valid C variable name?

Options:
A) _var
B) 1var
C) var1
D) Var

Answer: B) 1var (Variable names cannot start with a digit.)


3. What is the output of the following C code?
c
#include <stdio.h>
int main() {
int x = 5;
printf("%d", x++ + ++x);
return 0;
}

Options:
A) 10
B) 11
C) 12
D) Undefined behavior

Answer: D) Undefined behavior (Modifying x twice between sequence points leads to UB.)

4. Which operator is used for dynamic memory allocation in C++?

Options:
A) malloc
B) new
C) alloc
D) create

Answer: B) new
5. What does the following C code print?
c
#include <stdio.h>
int main() {
int a = 10, b = 20;
if (a = b) printf("Equal");
else printf("Not Equal");
return 0;
}

Options:
A) Equal
B) Not Equal
C) Compilation Error
D) Runtime Error

Answer: A) Equal (Assignment a = b returns 20 (true), so "Equal" is printed.)

6. What is the output of the following C++ code?


cpp
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 2;
cout << (x << y) << endl;
return 0;
}

Options:
A) 10
B) 20
C) 7
D) 3

Answer: B) 20 (Left-shifting 5 by 2 gives 5 * 2² = 20.)


7. Which of the following is not a storage class in C?

Options:
A) auto
B) register
C) static
D) public

Answer: D) public (It is a C++ access specifier, not a C storage class.)

8. What is the output of the following C code?


c
#include <stdio.h>
int main() {
printf("%d", 5 == 5 == 5);
return 0;
}

Options:
A) 1
B) 0
C) 5
D) Syntax Error

Answer: B) 0 (First 5 == 5 evaluates to 1, then 1 == 5 is 0.)

9. What does #include <stdio.h> do in a C program?

Options:
A) Includes standard input/output functions
B) Defines main function
C) Allocates memory
D) Declares variables
Answer: A) Includes standard input/output functions

10. What is the output of the following C++ code?


cpp
#include <iostream>
using namespace std;
int main() {
int a = 5;
int &r = a;
r++;
cout << a;
return 0;
}

Options:
A) 5
B) 6
C) Compilation Error
D) Runtime Error

Answer: B) 6 (Reference r modifies a directly.)

11. Which of the following is not a valid C data type?

Options:
A) float
B) double
C) real
D) char

Answer: C) real (Not a standard C type.)


12. What is the output of the following C code?
c
#include <stdio.h>
int main() {
int x = 0;
if (x = 1) printf("True");
else printf("False");
return 0;
}

Options:
A) True
B) False
C) Compilation Error
D) Runtime Error

Answer: A) True (Assignment x = 1 returns 1 (true).)

13. What is the correct way to declare a pointer in C?

Options:
A) int ptr;
B) int *ptr;
C) *int ptr;
D) ptr int;

Answer: B) int *ptr;


14. What is the output of the following C++ code?
cpp
#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3};
cout << arr[3];
return 0;
}

Options:
A) 3
B) 0
C) Garbage Value
D) Runtime Error

Answer: C) Garbage Value (Accessing out-of-bounds index leads to undefined behavior.)

15. Which keyword is used to prevent a variable from being modified in C?

Options:
A) const
B) static
C) final
D) fixed

Answer: A) const
16. What is the output of the following C code?
c
#include <stdio.h>
int main() {
printf("%d", 10 ? 0 ? 5 : 3 : 8);
return 0;
}

Options:
A) 5
B) 3
C) 8
D) 0

Answer: B) 3 (Nested ternary operator: 10 ? (0 ? 5 : 3) : 8 → 10 ? 3 : 8 → 3.)

17. Which function is used to deallocate memory in C?

Options:
A) free()
B) delete
C) remove()
D) dealloc()

Answer: A) free()
18. What is the output of the following C++ code?
cpp
#include <iostream>
using namespace std;
int main() {
int x = 5;
cout << (x > 2 ? "Yes" : "No");
return 0;
}

Options:
A) Yes
B) No
C) 1
D) Compilation Error

Answer: A) Yes (Ternary operator evaluates to "Yes" since 5 > 2.)

19. What is the size of an empty class in C++?

Options:
A) 0
B) 1
C) 4
D) Compilation Error

Answer: B) 1 (Empty classes have a size of 1 for unique addressability.)


20. What is the output of the following C code?
C
#include <stdio.h>
int main() {
int a = 5, b = 10;
printf(“%d”, a + b);
return 0;
}

Options:
A) 5
B) 10
C) 15
D) 510

Answer: C) 15 (Simple addition of 5 + 10.)

21. Which header file is needed for printf() in C?

Options:
A) <conio.h>
B) <stdlib.h>
C) <stdio.h>
D) <math.h>

Answer: C) <stdio.h>
Explanation: The printf() function is part of the Standard Input/Output library,
included via <stdio.h>.
22. What is sizeof(int) typically on a 32-bit system?

Options:
A) 1
B) 2
C) 4
D) 8

Answer: C) 4
Explanation: On a 32-bit system, int is usually 4 bytes. On 64-bit, it may still be 4
(but long becomes 8).

23. What does ++x do?

Options:
A) Post-increment
B) Pre-increment
C) Adds 2 to x
D) Returns x unchanged

Answer: B) Pre-increment
Explanation: ++x increments x before its value is used, while x++ increments after.

24. What is the output of printf("%c", 65);?

Options:
A) 65
B) A
C) Compilation Error
D) Garbage Value
Answer: B) A
Explanation: %c prints the ASCII character corresponding to the number (65 = 'A').

25. Which is not a loop in C?

Options:
A) for
B) while
C) foreach
D) do-while

Answer: C) foreach
Explanation: foreach is not a C/C++ loop (it exists in C#/Java/Python). C
uses for, while, and do-while.

26. What is the output of 5 / 2 in C?

Options:
A) 2
B) 2.5
C) 3
D) 2.0

Answer: A) 2
Explanation: Integer division truncates the decimal part. Use 5.0 / 2 to get 2.5.

27. What is NULL in C?

Options:
A) A macro for 0
B) A reserved keyword
C) A void pointer
D) An integer constant
Answer: A) A macro for 0
Explanation: NULL is typically defined as ((void *)0) or just 0 in C.

28. Which is a valid way to declare a constant in C?

Options:
A) constant int x = 5;
B) const int x = 5;
C) #define x 5
D) Both B and C

Answer: D) Both B and C


Explanation:

29. What is the output of printf("%d", !5);?

Options:
A) 5
B) 1
C) 0
D) -1

Answer: C) 0
Explanation: The logical NOT operator (!) returns 0 for non-zero values (like 5)
and 1 for 0.

30. What is the entry point of a C program?

Options:
A) start()
B) main()
C) init()
D) void min()

Answer: B) main()

Explanation: The standard entry point in C/C++ is int main(). void main() is non-standard.

You might also like