Basic Programming Questions
Basic Programming Questions
Explanation: Low level programming languages are machine dependent while the others
are high level programming languages
3. The process of writing a program in small independent parts called modules is ___.
a. Procedural programming c. Object oriented programming
b. Structured programming d. Sequential programming
4. A program should be in a way that can be modified or upgraded when the need
arises. This property best describe by ______
a. Portable c. Reliable
b. Efficient d. Maintainable
Explanation: It is maintainable
Explanation: Bjarne Stroustrup is the original creator of C++ in 1979 at AT&T Bell Labs.
8. What is C++?
a) C++ is an object oriented programming language
b) C++ is a procedural programming language
c) C++ supports both procedural and object oriented programming language
d) C++ is a functional programming language
Explanation: C++ supports both procedural (step by step instruction) and object oriented
programming (using the concept of classes and objects).
9. Which of the following is the correct syntax of including a user defined header files
in C++?
a) #include [userdefined]
b) #include “userdefined”
c) #include <userdefined.h>
d) #include <userdefined>
Explanation: C++ uses double quotes to include a user-defined header file. The correct
syntax of including user-defined is #include “userdefinedname”.
Explanation: Both the ways are used for commenting in C++ programming. // is used for
single line comments and /* … */ is used for multiple line comments.
11. Which of the following extension is used for user-defined header file in c++?
a) hg
b) cpp
c) h
d) hf
Explanation: .h extensions are used for user defined header files. To include a user defined
header file, one should use #include”name.h” i.e. it should be enclosed within double
quotes.
Explanation: Friend function is not a constructor whereas others are a type of constructor
used for object initialization.
15. What happens if the following C++ statement is compiled and executed?
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[])
{
char s1[6] = "Hello";
char s2[6] = "World";
char s3[12] = s1 + " " + s2;
cout<<s3;
return 0;
}
a) Hello
b) World
c) Error
d) Hello World
Answer: c
Explanation: There is no operation defined for the addition of character array in C++ hence
the compiler throws an error as it does not understood what to do about this expression.
#include <stdio.h>
int main(void)
{
int new = 5;
printf("%d", new);
}
a) Error in C and successful execution in C++
b) Error in both C and C++
c) Error in C++ and successful execution in C
d) A successful run in both C and C++
Explanation: new is a keyword in C++, therefore, we cannot declare a variable with name
new but as there is no such keyword new in C, therefore, the program is compiled and
executed successfully in C.
18. Which of the following C++ code will give error on compilation?
================code 1=================
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
cout<<"Hello World";
return 0;
}
========================================
================code 2=================
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout<<"Hello World";
return 0;
}
========================================
a) Code 1 only
b) Neither code 1 nor code 2
c) Both code 1 and code 2
d) Code 2 only
Explanation: Neither code 1 nor code 2 will give an error as both are syntactically correct as
in first code we have included namespace std and in second one we have used scope
resolution operator to resolve the conflict.
Answer: d
Explanation: C++ provides the boolean type to handle true and false values whereas no
such type is provided in C.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int p;
6. bool a = true;
7. bool b = false;
8. int x = 10;
9. int y = 5;
10. p = ((x | y) + (a + b));
11. cout << p;
12. return 0;
13. }
a) 12
b) 0
c) 2
d) 16
Explanation: Because array variable and values need to be declared after the datatype
only.
15. Which of the following symbol is used to declare the preprocessor directives in C++?
a) $
b) ^
c) #
d) *
1. #include <iostream>
2. using namespace std;
3. int main ()
4. {
5. int a, b, c;
6. a = 2;
7. b = 7;
8. c = (a > b) ? a : b;
9. cout << c;
10. return 0;
11. }
a) 12
b) 14
c) 6
d) 7
17. What will be the output of the following C++ code snippet?
1. #include <stdio.h>
2. #include<iostream>
3. using namespace std;
4. int main ()
5. {
6. int array[] = {0, 2, 4, 6, 7, 5, 3};
7. int n, result = 0;
8. for (n = 0; n < 8; n++)
9. {
10. result += array[n];
11. }
12. cout << result;
13. return 0;
14. }
a) 21 c) 26
b) 27 d) 25
Explanation: We are adding all the elements in the array and printing it. Total elements in
the array is 7, but our for loop will go beyond 7 and add a garbage value.