






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Certainly! Here are 20 C++ questions with errors for debugging, along with their respective answers: Question 1:
#include <iostream> int main() { int x = 5; std::cout << "The value of x is: " << x << std::endl; return 0; } Answer 1: The provided code is error-free. Question 2:
#include <iostream> int main() { int x = 10; std::cout << "The value of x is: " << x << std::endll; return 0; } ## ``` Answer 2: The error in this code is that `std::endll` is misspelled. It should be `std::endl` to properly output a new line. Question 3: ```cpp #include <iostream> int main() { int x; std::cout << "Enter the value of x: "; std::cin >> &x; std::cout << "The value of x is: " << x << std::endl; return 0; } Answer 3: The error in this code is that &x is used incorrectly with std::cin. It should be replaced with x to store the user input correctly. Question 4:
#include <iostream> int main() { int main() { float x = 5 / 2; std::cout << "The value of x is: " << x << std::endl; return 0; } Answer 6: The error in this code is that the integer division 5 / 2 will result in 2 instead of the expected 2.5. To fix this, change either 5 or 2 to 5.0 or 2.0 to perform floating-point division. Question 7:
#include <iostream> int main() { int x; std::cout << "Enter a number: "; std::cin >> x; std::cout << "The square of " << x << " is: " << x * x << std::endl; } Answer 7: The error in this code is that the main() function is declared as int, but it lacks a return statement. Adding return 0; at the end of the function will resolve this issue.
Question 8:
#include <iostream> int main() { int x = 5; std::cout << "The value of x is: " << x << std::endl; return 0; Answer 8: The error in this code is that the code block of main() is not closed with a closing brace }. Adding } at the end of the code will fix this issue. Question 9:
#include <iostream> int main() { int x, y; x = 5; std::cout << "The value of y is: " << y << std::endl; return 0; } Answer 9:
Answer 11: The error in this code is that the less-than operator < is used instead of the left shift operator << in std::cout statement. Replace < with << to correct the code. Question 12:
#include <iostream> void printValue(int value) { std::cout << "The value is: " << value << std::endl; } int main() { printValue(10) return 0; } Answer 12: The error in this code is that the function call printValue(10) is missing a semicolon ; at the end. Adding a semicolon will resolve this error. Question 13:
#include <iostream> int main() { int x = 10; if (x > 5) { std::cout << "x is greater than 5" << std::endl; } else std::cout << "x is less than or equal to 5" << std::endl; return 0; } Answer 13: The provided code is error-free. Question 14:
#include <iostream> int main() { int x = 20; if (x > 10) { std::cout << "x is greater than 10" std::endl; } else { std::cout << "x is not greater than 10" << std::endl; } return 0; #include <string> int main() { std::string name = "John"; std::cout << "The length of the name is: " << str.length() << std::endl; return 0; } Answer 16: The error in this code is that the variable name str is undefined. It should be replaced with name to correctly access the length of the string. Question 17:
#include <iostream> int main() { int x = 5; int y = 10; int sum; sum = x + y; std::cout << "The sum of x and y is: " << sun << std::endl; return 0; } Answer 17:
The error in this code is that sun is misspelled. It should be sum to correctly output the value of the sum. Question 18:
#include <iostream> int main() { int x = 5; int y = 10; int product; product = x * y; std::cout << "The sum of x and y is: " << product << std::endl; return 0; Answer 18: The error in this code is that the code block of main() is not closed with a closing brace }. Adding } at the end of the code will fix this issue. Question 19:
#include <iostream> int main() { int x = 5; int y = 10;