Table of contents | |
Multiple Choice Questions (MCQs) | |
High Order Thinking Questions (HOTS) | |
Fill in the Blanks | |
True or False | |
Hands-On Questions |
Q.1. Which of the following is true about functions in C++?
(a) Functions are used to perform repetitive tasks.
(b) Functions cannot return values.
(c) Functions can only be defined in the main function.
(d) Functions cannot be called from other functions.
Ans. (a)
Q.2. What is a function parameter?
(a) A variable used to store the return value of a function.
(b) A value passed to a function when it is called.
(c) A function that calls another function.
(d) A placeholder used for storing temporary values.
Ans. (b)
Q.3. Which statement is used to return a value from a function in C++?
(a) return
(b) yield
(c) exit
(d) break
Ans. (a)
Q.4. What is function overloading?
(a) Creating multiple functions with the same name but different return types.
(b) Creating multiple functions with the same name but different parameter lists.
(c) Creating multiple functions with the same name and the same parameter lists.
(d) Creating a function that can be called from any other function.
Ans. (b)
Q.5. Which of the following is not a step in building an ATM application in C++?
(a) Creating a function for depositing money.
(b) Creating a function for withdrawing money.
(c) Creating a function for displaying the account balance.
(d) Creating a function for sending emails.
Ans. (d)
Q.1. Write a C++ function named calculateArea that takes two parameters, length and width, and returns the area of a rectangle. Use the function to calculate the area of a rectangle with length 5 and width 3.
#include <iostream>
int calculateArea(int length, int width) {
return length * width;
}
int main() {
int area = calculateArea(5, 3);
std::cout << "Area: " << area << std::endl;
return 0;
}
Q.2. Write a C++ function named findMax that takes three parameters, num1, num2, and num3, and returns the maximum of the three numbers. Use the function to find the maximum of 10, 25, and 17.
#include <iostream>
int findMax(int num1, int num2, int num3) {
int max = num1;
if (num2 > max) {
max = num2;
}
if (num3 > max) {
max = num3;
}
return max;
}
int main() {
int maximum = findMax(10, 25, 17);
std::cout << "Maximum: " << maximum << std::endl;
return 0;
}
Q.3. Write a C++ function named isPalindrome that takes a parameter word and returns true if the word is a palindrome (reads the same forwards and backwards), and false otherwise. Use the function to check if "radar" is a palindrome.
#include <iostream>
#include <string>
bool isPalindrome(const std::string& word) {
int length = word.length();
for (int i = 0; i < length / 2; i++) {
if (word[i] != word[length - i - 1]) {
return false;
}
}
return true;
}
int main() {
std::string word = "radar";
if (isPalindrome(word)) {
std::cout << "Palindrome" << std::endl;
} else {
std::cout << "Not a palindrome" << std::endl;
}
return 0;
}
Q.4. Write a C++ function named power that takes two parameters, base and exponent, and returns the result of raising the base to the exponent. Use the function to calculate 2 raised to the power of 5.
#include <iostream>
int power(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main() {
int result = power(2, 5);
std::cout << "Result: " << result << std::endl;
return 0;
}
Q.5. Write a C++ function named factorial that takes a parameter n and returns the factorial of the number. Use the function to calculate the factorial of 6.
#include <iostream>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int result = factorial(6);
std::cout << "Factorial: " << result << std::endl;
return 0;
}
Q.1. A _______ is a named block of code that performs a specific task.
Ans. function
Q.2. A _______ is a value passed to a function when it is called.
Ans. parameter
Q.3. The _______ statement is used to return a value from a function in C++.
Ans. return
Q.4. Function _______ is the process of creating multiple functions with the same name but different parameter lists.
Ans. overloading
Q.5. Recursion is the process of a function calling itself _______.
Ans. recursively
Q.1. Functions in C++ are used to perform repetitive tasks.
Ans. True
Q.2. A function parameter is a value passed to a function when it is called.
Ans. True
Q.3. The return statement in C++ is used to exit the program.
Ans. False
Q.4. Function overloading in C++ involves creating multiple functions with the same name but different return types.
Ans. False
Q.5. Recursion is a technique where a function calls itself either directly or indirectly.
Ans. True
Q.1. Write a C++ function named isPrime that takes a parameter num and returns true if the number is prime, and false otherwise. Test the function with the number 17.
#include <iostream>
bool isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
int number = 17;
if (isPrime(number)) {
std::cout << number << " is prime." << std::endl;
} else {
std::cout << number << " is not prime." << std::endl;
}
return 0;
}
Q.2. Write a C++ function named printPattern that takes a parameter rows and prints the following pattern:
*
**
***
****
*****
Test the function with 5 rows.
#include <iostream>
void printPattern(int rows) {
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
std::cout << "*";
}
std::cout << std::endl;
}
}
int main() {
int rows = 5;
printPattern(rows);
return 0;
}
Q.3. Write a C++ function named reverseString that takes a parameter str and returns the reverse of the input string. Test the function with the string "hello".
#include <iostream>
#include <string>
std::string reverseString(const std::string& str) {
std::string reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str[i];
}
return reversed;
}
int main() {
std::string str = "hello";
std::string reversed = reverseString(str);
std::cout << "Reversed: " << reversed << std::endl;
return 0;
}
Q.4. Write a C++ function named countVowels that takes a parameter str and returns the number of vowels in the input string. Test the function with the string "programming".
#include <iostream>
#include <string>
int countVowels(const std::string& str) {
int count = 0;
std::string vowels = "aeiouAEIOU";
for (char c : str) {
if (vowels.find(c) != std::string::npos) {
count++;
}
}
return count;
}
int main() {
std::string str = "programming";
int vowelCount = countVowels(str);
std::cout << "Vowel count: " << vowelCount << std::endl;
return 0;
}
Q.5. Write a C++ function template named swapValues that takes two parameters, a and b, and swaps their values. Test the function template with integers and floating-point numbers.
#include <iostream>
template <typename T>
void swapValues(T& a, T& b) {
T temp = a;
a = b;
b = temp;
}
int main() {
int a = 10, b = 20;
std::cout << "Before swap: a = " << a << ", b = " << b << std::endl;
swapValues(a, b);
std::cout << "After swap: a = " << a << ", b = " << b << std::endl;
float x = 3.14, y = 2.71;
std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;
swapValues(x, y);
std::cout << "After swap: x = " << x << ", y = " << y << std::endl;
return 0;
}
70 videos|45 docs|15 tests
|
|
Explore Courses for Software Development exam
|