Table of contents | |
Instructions | |
Multiple Choice Questions (MCQs) | |
Higher Order Thinking Questions (HOTS) | |
Fill in the Blanks | |
True or False | |
Hands-On Questions |
Answer the following questions by selecting the correct option or filling in the blanks with the appropriate answers. For hands-on questions, write the code snippets to fulfill the given requirements
Q.1. Which of the following is NOT a valid C++ data type?
(a) float
(b) string
(c) double
(d) char
Ans. (b)
Q.2. What is the size of the "int" data type in C++?
(a) 4 bytes
(b) 2 bytes
(c) 8 bytes
(d) It varies depending on the system
Ans. (d)
Q.3. Which of the following is the correct way to declare a variable in C++?
(a) var x;
(b) x = 5;
(c) int x;
(d) 5 = x;
Ans. (c)
Q.4. What is the value of the variable "result" after executing the following code snippet?
int num1 = 10;
int num2 = 3;
double result = num1 / num2;
(a) 3.0
(b) 3
(c) 3.3333
(d) Compilation error
Ans. (b)
Q.5. Which operator is used to find the remainder of a division operation in C++?
(a) /
(b) %
(c) *
(d) $
Ans. (b)
Q.1. Write the correct C++ code to swap the values of two variables, x and y, without using a temporary variable.
View Answerint x = 5;
int y = 10;
x = x + y;
y = x - y;
x = x - y;
Q.2. Explain the difference between declaring a variable and initializing a variable.
View AnswerDeclaring a variable means creating its name and specifying the data type, while initializing a variable means assigning a value to it.
Q.3. What are the basic differences between the "float" and "double" data types in C++?
The "float" data type in C++ is a single-precision floating-point type that occupies 4 bytes of memory, while the "double" data type is a double-precision floating-point type that occupies 8 bytes of memory. The "double" data type can represent larger and more precise decimal numbers compared to "float."
Q.4. Write a C++ code snippet to calculate the area of a circle, given the radius as input from the user.
#include <iostream>
using namespace std;
int main() {
double radius;
double area;
const double pi = 3.14159;
cout << "Enter the radius of the circle: ";
cin >> radius;
area = pi * radius * radius;
cout << "The area of the circle is: " << area << endl;
return 0;
}
Q.5. How can you convert an integer variable to a string in C++? Provide an example.
#include <iostream>
#include <string>
using namespace std;
int main() {
int num = 12345;
string str = to_string(num);
cout << "The converted string is: " << str << endl;
return 0;
}
Q.1. The process of reserving memory space for a variable is called __________.
Ans. Allocation
Q.2. The _________ data type in C++ is used to store single characters.
Ans. char
Q.3. The _________ operator is used to access the memory address of a variable.
Ans. & (ampersand)
Q.4. The __________ function in C++ is used to read input from the user.
Ans. cin
Q.5. A variable that can hold the address of another variable is called a ________ variable.
Ans. pointer
Q.1. In C++, the variable names are case-insensitive.
Ans. False
Q.2. The "cin" statement is used to output data in C++.
Ans. False
Q.3. A character variable can store multiple characters.
Ans. False
Q.4. The "sizeof" operator returns the size of a variable in bytes.
Ans. True
Q.5. All variables in C++ must be declared before they can be used.
Ans. True
Q.1. Write a C++ program that declares two integer variables, x and y, and assign values of your choice to them. Print their sum.
#include <iostream>
using namespace std;
int main() {
int x = 5;
int y = 10;
int sum = x + y;
cout << "The sum of x and y is: " << sum << endl;
return 0;
}
Q.2. Write a C++ program that calculates and prints the average of three floating-point numbers input by the user.
#include <iostream>
using namespace std;
int main() {
float num1, num2, num3;
float average;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
average = (num1 + num2 + num3) / 3;
cout << "The average is: " << average << endl;
return 0;
}
Q.3. Write a C++ program that prompts the user to enter their name and age. Then, print a message that says "Hello, <name>! You are <age> years old."
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
int age;
cout << "Enter your name: ";
cin >> name;
cout << "Enter your age: ";
cin >> age;
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
return 0;
}
Q.4. Write a C++ program that takes a temperature value in Celsius and converts it to Fahrenheit. The formula to convert Celsius to Fahrenheit is: F = (C * 9/5) + 32.
#include <iostream>
using namespace std;
int main() {
double celsius, fahrenheit;
cout << "Enter temperature in Celsius: ";
cin >> celsius;
fahrenheit = (celsius * 9 / 5) + 32;
cout << "Temperature in Fahrenheit: " << fahrenheit << endl;
return 0;
}
Q.5. Write a C++ program that calculates and prints the factorial of a number entered by the user. The factorial of a non-negative integer n is denoted by n!, and it is the product of all positive integers less than or equal to n.
#include <iostream>
using namespace std;
int main() {
int number;
int factorial = 1;
cout << "Enter a number: ";
cin >> number;
for (int i = 1; i <= number; i++) {
factorial *= i;
}
cout << "The factorial of " << number << " is: " << factorial << endl;
return 0;
}
70 videos|45 docs|15 tests
|
1. What are variables in software development? |
2. How are data types used in programming? |
3. What is the difference between local and global variables? |
4. How do you declare a variable in programming languages like C++ or Java? |
5. Why is it important to choose the correct data type for a variable? |
|
Explore Courses for EmSAT Achieve exam
|