0% found this document useful (0 votes)
50 views

Assignment PLC and Oop 2023

This document contains an assignment on PLC & OOP programming with 25 questions. The questions cover topics like data types, operators, control flow, functions, classes, and relationships between objects. The student provides answers to questions on variable naming, arithmetic operations, loops, conditional statements, functions, classes and more. Justification is provided for concepts like operator overloading and encapsulation.

Uploaded by

Hafsa Shykh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Assignment PLC and Oop 2023

This document contains an assignment on PLC & OOP programming with 25 questions. The questions cover topics like data types, operators, control flow, functions, classes, and relationships between objects. The student provides answers to questions on variable naming, arithmetic operations, loops, conditional statements, functions, classes and more. Justification is provided for concepts like operator overloading and encapsulation.

Uploaded by

Hafsa Shykh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PLC & OOP

Assignment (Last Date 10th May 2023)


1. Before taking this course, did you have any prior programming experience?
Ans: Yes.
2. What type of challenges do you generally face while programming?
Ans: Challenges which I face in programming are like I make tons of mistakes in syntax or stuck in
common problems such as not putting semi colon, or sometimes curly brackets and also in codding.
3. Why study programming?
Ans: programming language are vital elements in today’s software development. with programming
skills you can design applications. programming can help you to understand how many things work
around you. And there is huge scope of programming in modern life.
4. Which programming concept do you feel is the most difficult to learn?
Ans: According to my perspective loop concept is little bit difficult in c++. And understanding the
concepts of polymorphism is also sometimes difficult for me.
5. What kind of material helped you most in learning programming?
Ans: As my basic is not clear so I learn c++ from my aunt whenever I have any difficulty or there is
an
app “codecademy” which is very helpful to clear my doubts and the best way to learn programming is
by doing it or by practicing it.
6. Is programming more important for human life? (Justify your answer).
Ans: yes, programming is more important for human life in modern world as it powers many of the
technology and systems that we rely on. Like Artificial intelligence, facilitating data analysis and
much more.
7. Write down five valid variable identifier names:
Ans: a) age
b) salary_per_hour
c) numberOfStudents
d) student_name
e) studentEmail

8. Write down the five invalid variable identifier names:


Ans: a) 123hafsa
b) student name
c) student-id
d) @email
e) class
______________________________________________________________________________
9. Create variable of following data types:
a. Integer:
b. Float:
c. Double:
d. String:
e. Character:
f. Boolean
Ans:
a) Integer:
int age=22;
cout<<age;
b) Float:
float price=10.78;
cout<<price;
c) Double:
double height=5.6;

cout<<height;

d) String:
string stdname="hafsa";
cout<<stdname;
e) Character:
char grade='A';
cout<<grade;
f) Boolean
bool isexam=true;
cout<<isexam;
10. int a = 7, b = 2;
int result= (++b) -5;
what is the value of result?
Ans: result = -2

11. int num1 = 5, num2 = num1 + 5; value of num2 will be _________.


Ans: 10
12. (( 5 == 5) || ( 3 >6 )) =_______?
true || false
OR
1 || 0
Answer is true or 1.
13. What will be output of (7+3)!=(100%10)
Ans: true or 1.
14. After execution of given statements, what will be output:
char letter=(Z – 24);
cout<<(char)letter<<endl:
Ans: Z = 90
Z – 24 = 66

So output will be character B.

15. What will be output of ((10)!=(100%2)) || (5==5)


Ans:

true || true

OR
1 || 1
Output is true or 1.
16. What will be value of temp after execution of given statements:
double temp=0;
int x=20.78;
int y=3;
temp=x+y;
cout<<temp;
Ans: 23.78
17. A person deposited one million in the bank. The bank pays an interest of 10%. Write a program
that compute his amount after three year.

Program,

#include <iostream>

#include <cmath>

using namespace std;

int main()
{
double principal = 1000000.0;
double rate = 0.10;
int time = 3;

double amount = principal *pow(1 + rate, time);

cout << "After " << time << " years, you will have $" << amount << endl;

return 0;
}

Output: After 3 years, you will have $1.331e+006

18. find the sum of following sequence of numbers using for loop:
10,20,30,40-------------1000

Program,

#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for (int i = 10; i <= 1000; i += 10) {
sum += i;
}
cout << "The sum of the sequence is: " << sum << endl;
return 0;
}
Output: The sum of the sequence is: 50500
19. Write a program that compute the sum of given sequence using for loop:
5,10,15------------5000
Hint : n=((an-a1)/d +1) Sn=n(a1+an)/2

Program,

#include <iostream>
using namespace std;
int main()
{
int a1 = 5;
int an = 5000;
int d = 5;
int n = ((an - a1) / d) + 1;
int sum = n * (a1 + an) / 2;
cout << "The sum of the sequence is: " << sum << endl;
return 0;
}
Output: The sum of the sequence is: 2502500
20. Write a program to separate the integral and fractional parts of a given real number and print the
result on screen.

Program,

#include <iostream>
using namespace std;
int main() {
double num;
int integral, fractional;
cout << "Enter a real number: ";
cin >> num;
integral = static_cast<int>(num);
fractional = static_cast<int>((num - integral) * 100);
cout << "Integral part: " << integral << endl;
cout << "Fractional part: " << fractional << endl;
return 0;
}
output:
Enter a real number: 56.34
Integral part: 56
Fractional part: 34
21. Write a program to input an integer value and find out whether integer is divisible by 3 or not
using conditional operator.

Program,

#include <iostream>
using namespace std;
int main() {
int num;

cout << "Enter an integer value: ";


cin >> num;

(num % 3 == 0) ? cout << num << " is divisible by 3" : cout << num << " is not divisible by 3";

return 0;
}

Output:
Enter an integer value: 24
24 is divisible by 3

22. Write a program to check whether a given character is letter or digit.


#include <iostream>
using namespace std;

int main() {
char ch;
cout << "Enter a character: ";
cin >> ch;
if (isalpha(ch)) {
cout << ch << " is a letter." << endl;
} else if (isdigit(ch)) {
cout << ch << " is a digit." << endl;
} else {
cout << ch << " is not a letter or digit." << endl;
}
return 0;
}
Output: Enter a character: y
y is a letter.

23. Write a program to input an integer number and display the corresponding name of the month by
using switch statement.

For example, if an integer value 3 is entered then print “March” and so on. If value is not between
1 and 12 then print message “Invalid Month”.

Program,

#include <iostream>
using namespace std;
int main() {
int month;
cout << "Enter a number between 1 and 12: ";
cin >> month;
switch(month) {
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << "December";
break;
default:
cout << "Invalid Month";
}

return 0;
}
Output:
Enter a number between 1 and 12: 7
July
24. What’s reason to overload the operators? and why passing an object reference in operator
overloading in C++, see in below code.

Ans: Operator overloading allows operators to be used with user-defined types just like they are used
with primitive types. This makes code more readable and expressive.
Passing an object reference in operator overloading in C++ is necessary because operator overloading
is typically done as a member function of a class, which operates on an object of that class. By
passing the object reference as a parameter, the operator function can modify the object's state as
necessary. Additionally, passing by reference is more efficient than passing by value since it avoids
unnecessary copying of the object.

25. What is the importance of encapsulation in object oriented programming and how it is achieved
or implemented?
Ans: Encapsulation is an important concept in object-oriented programming that refers to the
bundling of data and methods within a single unit, i.e., a class, and restricting access to the data from
outside the class. Encapsulation allows for data hiding and protects the data from being modified by
unauthorized users. This helps in maintaining the integrity and reliability of the program.
Encapsulation is achieved in C++ through the use of access specifiers, such as public, private, and
protected, which restrict the access to the members of the class. By default, all members of a class are
private, and can only be accessed within the class itself. However, by making certain members public
or protected, they can be accessed from outside the class as well.
26. Write down names and notations of four types of relationship between objects and also write
down two examples of each relationship.

Ans: Four types of relationships between objects in object-oriented programming are:

1. Association (uses-a relationship):


The notation for association is a solid line between two classes with an arrow indicating the
direction of the relationship.
Examples: A car uses an engine, a student uses a book.

2. Aggregation (has-a relationship):


The notation for aggregation is a hollow diamond at the end of a solid line, pointing to the class
that contains the other object.
Examples: A university has departments, a car has wheels.

3. Composition (part-of relationship):


The notation for composition is a filled diamond at the end of a solid line, pointing to the class
that contains the other object.
Examples: A car has an engine, a computer has a motherboard.

4. Inheritance (is-a relationship):


The notation for inheritance is a solid line with a hollow triangle at the end, pointing to the base
class.
Examples: A car is a vehicle, a dog is an animal.

27. There are many types of classes in object oriented programming. Write down the five names of
classes.
Ans: five names of classes in object-oriented programming:
1. Abstract class
2. Concrete class
3. Final class
4. Static class
5. Inner class

28. What is exception handling? Also enlist five examples of exceptions?


Ans: Exception handling is a mechanism used in programming to handle errors or exceptional
situations that can occur during program execution. It allows programmers to handle the errors and
exceptions gracefully, preventing the program from crashing.
Examples:
1. ArithmeticException
2. NullPointerException
3. ArrayIndexOutOfBoundsException
4. FileNotFoundException
5. OutOfMemoryError
29. Create a static function, named intToString(int N), that convert integer value into string.

Ex : Input : N=107067
Output: “107067”
Program,

#include <iostream>
#include <string>

using namespace std;

class mystring {
public:
static string intToString(int N) {
string str=" ";
bool is_negative=false;

if(N<0){
is_negative=true;
N=-N;
}
while(N>0){
int digit=N%10;
str=char(digit)+str;
N/=10;
}
if(is_negative){
str='-'+str;
}
return str;
}

};
int main() {
int num = 107067;
string str=mystring::intToString(num);
cout <<"the string representation of"<<num<<"is"<<str<<endl;
return 0;
}
Output:
the string representation of107067is107067

Ascii (American Standard Code for Information Interchange)

0 1 2 3 4 5 6 7 8 9

48 49 50 51 52 53 54 55 56 57

______________________________________________________________________________

You might also like