Chapter 1: Introduction to Object Oriented Programming
- Principles of Object Oriented Programming
- Introduction to Java
Content
------------------------------------------------------------------------------------------------------------------------------------------------------
1. Evolution to Programming Language2. Programming Paradigm
3. Principles of Object Oriented Programming
4. All Principles and Working Together
5. Difference between Procedural Oriented and Object Oriented.
------------------------------------------------------------------------------------------------------------------------------------------------------
Computer Language
Types of Computer Language
- High Level Language:
- Most programming languages that you will have heard of are high-level languages.
- Python and C# are examples of high-level languages that are widely used in education and in the workplace.
- A high-level language is one that is user-oriented in that it has been designed to make it straightforward for a programmer to convert an algorithm into program code.
- Low Level Language:
- A low-level language is machine-oriented.
- Low-level programs are expressed in terms of the machine operations that must be performed to carry out a task.
- This makes writing programs more difficult, as the algorithm must be specified in terms of the capabilities and specifications of the processor.
- Low-level languages are named for the processor (or processor family) that they are designed for, and are often referred to as assembly language or machine code.
- Assembly Language:
- Low-level languages are named for the processor (or processor family) that they are designed for, and are often referred to as assembly language or machine code.
- Compiler and Interpreter
- A compiler translates code written in a high-level programming language into a lower-level language like assembly language, object code and machine code (binary 1 and 0 bits).
- It converts the code ahead of time before the program runs.
- An interpreter translates the code line-by-line when the program is running
Types of High Level language
- Structural Oriental Programming Language
- encourages dividing an application program into a hierarchy of modules or autonomous elements, which, in turn, may contain other such elements
- Within each element, code may be further structured using blocks of related logic designed to improve readability and maintainability.
- Example :
- Procedural Oriental Programming Language
- The procedural oriented approach allows the user to develop their logic using a number of functions that would enhance the program's productivity.
- Conventional Programming using high level language such as Fortran Pascal C and COBOL.
- Object Oriental Programming Language
- It is an approach to standardized the program by creating partitioned memory area for both data and functions.
Question Answers:-
Q1. What are OOPs Concepts?
Ans: The object oriented concepts are:-
Class
Objects
Data Abstraction
Encapsulation
Inheritance
Polymorphism
Dynamic Binding
Message Passing
Q2. What is class?
Ans:- A class is a user-defined data type. It consists of data members and member functions, which can be accessed and used by creating an instance of that class. It represents the set of properties or methods that are common to all objects of one type. A class is like a blueprint for an object.
For Example: Consider the Class of Cars. There may be many cars with different names and brands but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here, Car is the class, and wheels, speed limits, mileage are their properties.
Q3. What is Object?
Ans:-
It is a basic unit of Object-Oriented Programming and represents the real-life entities. An Object is an instance of a Class. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated. An object has an identity, state, and behavior. Each object contains data and code to manipulate the data. Objects can interact without having to know details of each other’s data or code, it is sufficient to know the type of message accepted and type of response returned by the objects.
For example Dog is a real-life Object, which has some characteristics like color, Breed, Bark, Sleep, and Eats.
Object in OOPs
Object
Q4. What is Data Abstraction?
Ans : Data abstraction is one of the most essential and important features of object-oriented programming. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation. Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of the car or applying brakes will stop the car, but he does not know about how on pressing the accelerator the speed is increasing, he does not know about the inner mechanism of the car or the implementation of the accelerator, brakes, etc in the car. This is what abstraction is.
Q5. What is Encapsulation?
Ans:- Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. In Encapsulation, the variables or data of a class are hidden from any other class and can be accessed only through any member function of their class in which they are declared. As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
Encapsulation in Object Oriented Programming
Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section, etc. The finance section handles all the financial transactions and keeps records of all the data related to finance. Similarly, the sales section handles all the sales-related activities and keeps records of all the sales. Now there may arise a situation when for some reason an official from the finance section needs all the data about sales in a particular month. In this case, he is not allowed to directly access the data of the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of the sales section and the employees that can manipulate them are wrapped under a single name sales section.
Q6. What is Inheritance?
Ans:- Inheritance is an important pillar of OOP(Object-Oriented Programming). The capability of a class to derive properties and characteristics from another class is called Inheritance. When we write a class, we inherit properties from other classes. So when we create a class, we do not need to write all the properties and functions again and again, as these can be inherited from another class that possesses it. Inheritance allows the user to reuse the code whenever possible and reduce its redundancy.
Inheritance in Object Oriented Programming
Q7. What is Polymorphism?
Ans:- The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. For example, A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person posses different behavior in different situations. This is called polymorphism.
Q8. What is Dynamic Binding?
Ans:- In dynamic binding, the code to be executed in response to the function call is decided at runtime. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run time. Dynamic Method Binding One of the main advantages of inheritance is that some derived class D has all the members of its base class B. Once D is not hiding any of the public members of B, then an object of D can represent B in any context where a B could be used. This feature is known as subtype polymorphism.
Q9. What is Message Passing:
Ans:-
It is a form of communication used in object-oriented programming as well as parallel programming. Objects communicate with one another by sending and receiving information to each other. A message for an object is a request for execution of a procedure and therefore will invoke a function in the receiving object that generates the desired results. Message passing involves specifying the name of the object, the name of the function, and the information to be sent.
Q10. Why do we need object-oriented programming?
Ans:- To make the development and maintenance of projects more effortless.
To provide the feature of data hiding that is good for security concerns.
We can solve real-world problems if we are using object-oriented programming.
It ensures code re usability.
It lets us write generic code: which will work with a range of data, so we don’t have to write basic stuff over and over again.
1. Multiple Choice Questions:-
1. An _________is an identifiable entity with some characteristics and behavior.
(a) class (b) object (c) encapsulation (d) Inheritance
[Ans: (a) object]
2. _________ is the way of combining both data and the functions that operates on that data under a single unit.
(a) Inheritance (b) Polymorphism (c) Encapsulation (d) Data Binding
[Ans:(c) Encapsulation]
3.The keyword to create a class is
(a) Class (b) object (c) final (d) None
[Ans:(a) class]
4. Which of the following is not a feature of OOPS
(a) Inheritance (b) Function Overloading (c) creating method (d) All are OOPS Features
[Ans:(c) creating method]
5. Encapsulation means
(a) Binding of all functions together (b) Adding a function to a class
(c) Deleting a method from a class (d) None of the above
[Ans:(a) class]
6. A class is an object factory
(a) True (b) False
[Ans:(a) True]
7. A non static method is also called :
(a) class method (b) Instance method (c) Constant method (d) Instance variable
[Ans:(a) class method]
8. One thing in many different forms “ goes with the OOPS concept
(a) Polymorphism (b) Inheritance (c) Encapsulation (d) Abstraction
[Ans:(a) Polymorphism]
9. Data hiding is implemented by
(a) Access specifiers (b) Static variables (c) Inheritance (d) None
[Ans:(a) class]
10. Java supports
(a) Single Inheritance (b) Multilevel Inheritance (c) Multiple Inheritance (d) Both a and b
[Ans:(a) Single Inheritance]
(a) Class only (b) class and package (c) class, subclass and package (d) None
[Ans:(a) class]
(a) Private (b) Public (c) Protected (d) Package
13. Akash wants the members of his class "Employee" to be accessible only to his class "Employee", What access specifier he should use for the members
(a) Private (b) Public (c) Protected (d) default
14. A bundle of similar classes is called a
(a) Package (b) Group (c) Packed classes (d) None
15. Code reuse is implemented by :
(a) Abstraction
(b) Function overloading
(c) Inheritance
(d) Polymorphism
16. Which of the following are not keywords?
(a) NULL (b) Implements
(c) Protected (d) None of the above
17. Which of the following statements are true?
(a) An abstract class may not have any final methods?
(b) A final class may not have any abstracts methods.
(c) Transient variables must be static.
(d) All of the above
18. Which of the following methods belong to the String class?
(a) length() (b) compareTo()
(c) substring() (d) all of the them
19. What do you mean by nameless objects?
(a) An object created by using the new keyword.
(b) An object of a superclass created in the subclass.
(c) An object without having any name but having a reference.
(d) An object that has no reference.
20. Which of the following are primitive data types? (i) double (ii) String (iii) Char (iv) Integer
(a) (i) and (iii) (b) (ii) and (iii)
(c) Only (iii) (d) (i) and (iv)
21. Data type whose size is fixed are called data type
(a) primitive
(b) non-primitive
(c) explicit
(d) fixed
22. What is known as an object factory?
(a) instance (b) class
(c) data (d) function
From Question 23-27 find the odd one out :
23. (a) Encapsulation (b) Abstraction (c) Data Hiding (d) int
24. (a) Inheritance (b) Reusability (c) Base class and Sub class (d) private
25. (a) private (b) OOPS (c) default (d) public
26. (a) Data members (b) Characteristics (c) Properties (d) Functions
27. (a) Polymorphism (b) One thing in many forms (c) Abstraction (d) Function overloading
28. A private member of a class can be accessed only inside the class
(a) True (b) False
29. The default access specifier for a class member is public
(a) True (b) False
30. The OOPS concept that indicates data hiding is Polymorphism
(a) True (b) False
31. Default access specifier means package access specifier
(a) True (b) False
32. Class methods are called using class name
(a) True (b) False
33. The operator creates a new object and allocates memory for it
(a) create (b) add (c) allocate (d) new
34. The operator that deallocates the memory occupied by an object and deletes the object is
(a) remove (b) deallocate (c) delete (d) None
35. A static data member of a class is initialized in
(a) Inside the class (b) Inside a static method (c) Just after class definition (d) Inside main( ) method
36. To create an instance method the keyword used is
(a) Instance (b) Static (c) Non static (d) No keyword required
37. Static methods are called using
(a) Object (b) Class name (c) Any one (d) None
38. Seema wants a data member of her class “Product” to be available in all child classes, the access specifier she should use is
(a) private (b) protected (c) extends (d) inherits
39. Which statement is true about a class definition
(a) It must have at least one method
(b) It must have at least one data member
(c) It may not have any members
40. Which statement is True :
(a) An instance method can access only instance variables
(b) An instance method can access only static variables
(c) An instance method can access both static and non static data members
(d) None of the above
41. The keyword that represents the current object is
(a) now (b) this (c) current (d) None
42. The attributes or properties of a class are represented by :
(a) Member functions (b) Data members (c) Access specifiers (d) All of them
43. Class variable that is available to the entire class is known as
(a) local variable (b) class variable (c) instance variable (d) scope variable
44. The region within which a variable/piece-of-code is accessible called
(a) area of variable (b) place of variable (c) instance of variable (d) scope of variable
45. keyword is used to provide access to classes stored inside a package in our program.
(a) Import (b) this (c) export (d) public
46. A class is a/an of an object
(a) instance (b) blue print (c) design (d) none of the above
47. The statement to create an object of class student in Java is:
(a) obj= create Object(); (b) obj=new Student();
(c) obj= new object(Student); (d) None of the above
48. objects of a class can be created .
(a) 5 (b) Only 1 (c) As many (d) 2
49. Objects cannot be created of class.
(a) Concrete (b) Virtual (c) Base (d) Child
50. The parent class of a class is also called
(a) Base class (b) Super class (c) Both (a) and (b) (d) None
51. A class is accessible both inside and outside a package
(a) public (b) open (c) protected (d) All
52. To delete an object obj the statement is
(a) delete obj; (b) del obj; (c) remove obj; (d) delete [] obj;
53. A class is a
(a) Primitive data type (b) User defined data type (c) User defined derived data type (d) Derived data type
54. The other name for messages of a class is :
(a) Methods (b) Functions (c) Instances (d) Both a and b
55. The members of a class are defined inside
(a) ( ) (b) { } (c) [ ] (d) None of the above
56. Given a class as follows class product
{
public void display()
{ }
}
To invoke the display function using an object ob the statement is
(a) ob@display(); (b) ob.display(); (c) product.display(); (d) product.ob.display();
57.What is error in the following class definitions? abstract class xy {
abstract sum(int x, int y) {}}
(a) class header is not define properly
(b) constructor is no defined
(c) method is not defined properly
(d) no error
58.What should be the execution order, if a class has a method, static block, instance block, and constructor, as shown below?
public class First_C { public void myMethod()
{
System.out.println("Method");
}
{
System.out.println("Instance Block");
}
public void First_C()
{
System.out.println("Constructor");
}
static
{
System.out.println("static block");
}
public static void main(String[] args) { First_C c = new First_C();
c.First_C();
c.myMethod();
}
}
(a) Instance block, method, static block, and constructor
(b) Method, constructor, instance block, and static block
(c) Static block, method, instance block, and constructor
(d) Static block, instance block, constructor, and method
59. What will be the output of the following program? public class MyFirst {
public static void main(String[] args) { MyFirst obj = new MyFirst(n);
}
static int a = 10;
static int n; int b = 5; int c;
public MyFirst(int m) {
System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
}
// Instance Block
{
b = 30;
n = 20;
}
// Static Block static
{
a = 60;
}
}
(a) 10, 5, 0, 20, 0 (b) 10, 30, 20
(c) 60, 5, 0, 20 (d) 60, 30, 0, 20, 0
60. A class student is defined with following member function: gettotal() & takedata() and showdata() to get the sum of marks obtained from takadata and shown with showdata function.
Fill in the blanks in the JAVA code provided with appropriate options:
class student
{
private int admno;
private String sname;
private float eng, math, science, total; private float (a) ()
{
float t = eng + math + science; return t;
}
public void (b) ()
{
System.out.println(admno); System.out.println(sname); System.out.println(eng); System.out.println(math); System.out.println(science); System.out.println(total);
}
public void takedata(int a, String n, int e, int m, int s)
{
admno = a; sname = n; eng = e; math = m; science = s;
total = gettotal( );
}
}
public class stud
{
public static void main(String [] args)
{
student (c) = new student(); std1.takedata(101, "RAM", 30, 48, 40); std1.showdata();
}
}
(a)
(i) getdata
(ii) showdata
(iii) gettotal (b)
(i) getdata
(ii) showdata
(iii) gettotal (c)
(i) Std
(ii) std1
(iii) student
61. A static method is also called a :
(a) Instance function (b) Defined function (c) Class method (d) Fixed function
62. The concept of Abstraction is
(a) Binding data and functions together (b) Hiding inner complexity and providing usable interfaces
(c) Reusing of the code (d) Making methods constant
No comments:
Post a Comment