0% found this document useful (0 votes)
19 views8 pages

C++ Module 1

The document covers fundamental concepts of Object Oriented Programming (OOP) in C++, including abstraction, encapsulation, inheritance, and polymorphism. It also provides basic C++ syntax, user-defined types, and sample programs demonstrating simple operations like addition, checking even/odd numbers, and finding the largest number among three inputs. Additionally, it includes explanations for various programming constructs and operators used in C++.

Uploaded by

samarthmshetty2
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)
19 views8 pages

C++ Module 1

The document covers fundamental concepts of Object Oriented Programming (OOP) in C++, including abstraction, encapsulation, inheritance, and polymorphism. It also provides basic C++ syntax, user-defined types, and sample programs demonstrating simple operations like addition, checking even/odd numbers, and finding the largest number among three inputs. Additionally, it includes explanations for various programming constructs and operators used in C++.

Uploaded by

samarthmshetty2
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/ 8

Module-1

Introduction to Object Oriented Programming


1. The OOPs concept in C++, exposing only necessary information to users or clients is known as
1. Abstraction
2. Encapsulation
3. Data hiding
4. Hiding complexity
Answer: 1

2. Which of the following is an abstract data type?


1. Class
2. Int
3. String
4. Double
Answer: 1

3. Hiding the complexity is known as


1. Abstraction
2. Encapsulation
3. Data hiding
4. Composition
Answer: 2

4. What is the full form of OOP


1. Object oriented programming
2. Oriented object programming
3. Office oriented programming
4. office objective programming

5. What is the output of below program?


int main()
{
int a = 10;
cout<<a++;
return 0;
}
1. 10
2. 11
3. 12
4. Not defined

6. Which operator has highest precedence in * / %


1. *
2. /
3. %
4. all have same precedence

7. Wrapping data and its related functionality into a single entity is known as _____________
a) Abstraction b) Encapsulation c) Polymorphism d) Modularity
Answer: B
Explanation: In OOPs, the property of enclosing data and its related functions into a single entity(in C++ we call them
classes) is called encapsulation
8. How structures and classes in C++ differ?
a) In Structures, members are public by default whereas, in Classes, they are private by default
b) In Structures, members are private by default whereas, in Classes, they are public by default
c) Structures by default hide every member whereas classes do not
d) Structures cannot have private members whereas classes can have
Answer: A
Explanation: Structure members are public by default whereas, class members are private by default. Both of them can
have private and public members.

9. What does polymorphism in OOPs mean?


a) Concept of allowing overriding of functions
b) Concept of hiding data
c) Concept of keeping things in different modules/files
d) Concept of wrapping things into a single unit
Answer: A
Explanation: In OOPs, Polymorphism is the concept of allowing a user to override functions either by changing the types
or number of parameters passed.

10. Which concept allows you to reuse the written code?


a) Encapsulation
b) Abstraction
c) Inheritance
d) Polymorphism
Answer: C
Explanation: Inheritance allows you to reuse your already written code by inheriting the properties of written code into
other parts of the code, hence allowing you to reuse the already written code.

11. C++ is ______________


a) Procedural programming language
b) Object oriented programming language
c) Functional programming language
d) Both procedural and object oriented programming language
Answer: D
Explanation: C++ supports both procedural(step by step instruction) and object oriented programming(using concept of
classes and objects).

12. What does modularity mean?


a) Hiding part of program
b) Subdividing program into small independent parts
c) Overriding parts of program
d) Wrapping things into single unit
Answer: B
Explanation: Modularity means dividing a program into independent sub programs so that it can be invoked from other
parts of the same program or any other program.
13. Which of the following feature of OOP is not used in the following C++ code?
class A{
int i;
public:
void print()
{
cout << "hello" << i;
}
}
class B : public A{
int j;
public:
void assign (int a )
{
k = a;
}
}
a) Abstraction
b) Encapsulation
c) Inheritance
d) Polymorphism
Answer: D
Explanation: As i and j members are private i.e. they are hidden from outer world therefore we have used the concept of
abstraction. Next data members and there related functions are put together into single class therefore encapsulation is
used. Also as class B is derived from A therefore Inheritance concept is used. But as no function is overloaded in any of
the classes therefore, the concept of polymorphism is missing here.

14. How run-time polymorphisms are implemented in C++?


a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions
Answer: D
Explanation: Run-time polymorphism is implemented using Inheritance and virtual in which object decides which
function to call.

15. How compile-time polymorphisms are implemented in C++?


a) Using Inheritance
b) Using Virtual functions
c) Using Templates
d) Using Inheritance and Virtual functions
Answer: C
Explanation: Compile-time polymorphism is implemented using templates in which the types(which can be checked
during compile-time) are used decides which function to be called.
First C++ Program -Basic C++ syntax
1. Who developed C++?
a) Bjarne Stroustrup
b) Dennis Ritchie
c) Ken Thompson
d) Brian Kernighan
Answer: A
Explanation: Bjarne Stroustrup is the original creator of C++ during 1979 at AT&T Bell Labs.

2. Which one of the following is a keyword?


a) Size
b) Key
c) Jump
d) switch
Answer: D

3. ____ is the smallest individual unit in a program.


a) Variable
b) Control
c) Character
d) Token
Answer: D

4. Which of the following is the correct syntax of including a user defined header files in C++?
a) #include <userdefined.h>
b) #include <userdefined>
c) #include “userdefined”
d) #include [userdefined]
Answer: C
Explanation: C++ uses double quotes to include a user-defined header file. The correct syntax of including user-defined is
#include “userdefinedname”.

5. Which of the following is called address operator?


a) *
b) &
c) _
d) %
Answer: B
Explanation: & operator is called address operator and is used to access the address of a variable.

6. Which of the following is used for comments in C++?


a) // comment
b) /* comment */
c) both // comment or /* comment */
d) // comment */
Answer: C
Explanation: Both the ways are used for commenting in C++ programming. // is used for single line comments and /* …
*/ is used for multiple line comments.
7. What is a constant that contains a single character enclosed within single quotes?
a) Numeric
b) Fixed
c) Character
d) Floating Point
Answer: C

8. A C++ code line ends with ___


a) A Semicolon (;)
b) A Fullstop(.)
c) A Comma (,)
d) A Slash (/)
Answer: A

9. Which of the following escape sequence represents carriage return?


a) \r
b) \n
c) \n\r
d) \c
Answer: A
Explanation: \r is used to represent carriage return which means move the cursor to the beginning of the next line.

10. Which of the following is the correct difference between cin and scanf()?
a) both are the same
b) cin is a stream object whereas scanf() is a function
c) scanf() is a stream object whereas cin is a function
d) cin is used for printing whereas scanf() is used for reading input
Answer: B
Explanation: cin is a stream object available in C++ whereas scanf() is a function available in both C and C++. both are
used for reading input from users.
11. Which of the following is the scope resolution operator?
a) .
b) *
c) ::
d) ~
Answer: C
Explanation: :: operator is called scope resolution operator used for accessing a global variable from a function which is
having the same name as the variable declared in the function.

12. What will be the output of the following code snippet?


#include <stdio.h>
int main()
{
int sum = 2 + 4 / 2 + 6 * 2;
printf("%d", sum);
return 0;
}
14
18
16
56
13. Which data type is used to represent the absence of parameters?
a) int
b) short
c) void
d) float
Answer: C

14. Which type is best suited to represent the logical values?


a) integer
b) boolean
c) character
d) float
Answer: B

15. Identify the user-defined types from the following?


a) enumeration
b) classes
c) both enumeration and classes
d) int
Answer: C
Explanation: They must be defined by the users before use, unlike the other types which are readily available.

Sample programs:
1. Adding two numbers in C++.
Ans. Take two variables and take user input and add them.
#include <iostream>
int main() {
int a ;
int b ;
cin>>a>>b;
cout<<a+b;
return 0;
}
Input: 2 5
Output: 7

2. Check if a number is even or odd.


Ans. If the given number is divisible by 2 then it is even otherwise odd.
#include <iostream>
int main() {
int a ;
cin>>a;
if(a%2 == 0) // if remainder is zero then even number
cout<<”even”;
else cout<<”odd”;
return 0;
}
Input: 8
Output: even
3. Write a program to find the largest number among three numbers.
Ans. A number will be largest if number is greater than both the other numbers.
#include <iostream>
int main() {
float a, b, c;
cin >> a >> b >> c;
if(a >= b && a >= c)
cout << "Largest number: " << a;
if(b >= a && b >= c)
cout << "Largest number: " << b;
if(c >= a && c >= b)
cout << "Largest number: " << c;
return 0;
}
Input: 1 2 3
Largest number: 3

4.Find the sum of all the natural numbers from 1 to n.


Ans. We can do this using two ways, one is by iterating from 1 to n and adding them up while the other way is using
the summation formula – summation of i from 1 to n=n(n+1)/2=1+2+3+4+..+(n-1)+n
#include <iostream>
int main()
{
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << sum;
return 0;
}
Input: 5
Output: 15

5. Write a program to check whether a number is prime or not.


Ans. A prime number is not divisible by any number smaller than it except 1. Basically, it has only two factors 1 and itself.

#include <iostream>
int main() {
int a ;
cin>>a;
int b = 2;
//start from b as 1 can divide any number
bool prime = true;
while(b!=a){
if(a%b == 0)
{
prime = false;
break;
}
b++;
}
if(prime)
cout<<"prime";
else cout<<"not prime";
return 0;
}

6. Calculate the average of all the elements present in an array


Sol: average= summation(arr[i])/n
#include <iostream>
int main()
{
int n;
cin>>n;
int arr[n];
float sum = 0.0;
for(int i = 0;i<n;i++)
cin>>arr[i];
for(int i = 0;i<n;i++)
sum += arr[i];
cout<<(float)(sum/(float)n);
return 0;
}
Input: 3
145
Output: 3.33333

7. Write a function to find the length of a string in CPP.


Ans. Iterate through the string and increase count by 1 till we reach the end of the string.

#include <iostream>
int main()
{
string str;
cin>>str;
int count = 0;
for(int i = 0;str[i];i++) // till the string character is null
count++;
cout<<count;
}
Input: abcde
Output: 5

You might also like