Tuesday 1 February 2022

OOps Programming with C++ and JAVA , VMOU June-2021 Question 1


(i)Differentiate Exception and error?

Sr. No.

Key

Error

(हार्डवेयर संबंधी)

Exception

(कोडिंग संबंधी)

1

Type 

Classified as an unchecked type 

एरर केवल अनचैक्‍ड प्रकार की होती है।

Classified as checked and unchecked 

इनको चैक्‍ड व अनचैक्‍ड में विभक्‍त किया जा सकता है।

2

Package 

It belongs to java.lang.error 

यह जावा में Java Lang Error पैकेज में निहित होती है।

It belongs to java.lang.Exception 

यह जावा में Java Lang Exception पैकेज में निहित होती है।

3

Recoverable/ Irrecoverable

It is irrecoverable

इन्‍हें कोडिंग को ठीक करके Solve नहीं किया जा सकता है।

It is recoverable

इन्‍हें कोडिंग को ठीक करके(exception handling) Solve किया जा सकता है।

 

It can't be occur at compile time 

ये समस्‍याएं कंपाइल टाइम पर नहीं आती है।

It can occur at run time compile time both 

ये समस्‍याएं कंपाइल टाइम और रन टाइम दोनों पर ही आ सकती है।

5

Example

OutOfMemoryError ,IOError 

हार्डवेयर संबंधी समस्‍याएं जैसे उपलब्‍ध मेमोरी से अधिक आवश्‍यकता एरर, इनपुट आउटपुट से संबंधित एरर

NullPointerException , SqlException 

शून्य सूचक का अपवाद, डाटाबेस को एक्‍सेस करने से संबंधित अपवाद

(ii) How is class declared in c++? Give an Example.

·         A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end.

·         The class has a mechanism to prevent direct access to its members, which is the central idea of object-oriented programming. The class declaration is also known as formation of new abstract data type. The abstract data type can be used as basic data type such as int, float etc. The class consists of member variables that hold data and member functions that operate on the member variables of the same class.

Read Also

What is a Class?

·         A class is a blueprint or prototype that defines the variables and the methods common to all objects of a certain kind.

 

 

(iii) Write any four differences between C++ and JAVA.

KEY Difference between C++ and Java in Hindi:

  • C ++ केवल Compiler का उपयोग करता है, जबकि जावा compiler और interpreter दोनों का उपयोग करता है।
  • C ++ operator overloading और method overloading दोनों को सपोर्ट करता है जबकि Java केवल method overloading का समर्थन करता है।
  • C ++ नए और डिलीट कीवर्ड की मदद से मैन्युअल ऑब्जेक्ट मैनेजमेंट का समर्थन करता है जबकि Java में बिल्ट-इन ऑटोमैटिक garbagge collection होता है।
  • C ++ structures का समर्थन करता है जबकि जावा structures का समर्थन नहीं करता है।
  • C ++ unions का समर्थन करता है जबकि जावा unions का समर्थन नहीं करता है।

Comparison Index

C++

Java

Platform-independent

No

Yes

Goto

Statement Support

Yes

No

Multiple inheritance Support

Yes

Java, class के माध्यम से multiple inheritance का समर्थन नहीं करता है। यह Java में interfaces के द्वारा प्राप्त किया जा सकता है।
Java doesn't support multiple inheritance through class. It can be achieved by using interfaces in java

Operator Overloading Support

Yes

No

Pointers supports

Yes

No

Compiler and Interpreter

Compiler

Compiler and Interpreter

Call by Value and Call by reference

support

Both

call by value

Structure and Union

Supports

Yes

No

Thread Support

C ++ में threads के लिए built-in समर्थन नहीं है। यह thread-support के लिए third-party library पर निर्भर करता है।

C++ doesn't have built-in support for threads. It relies on third-party libraries for thread support.

जावा में built-in thread support है।

Java has built-in thread
support.

Documentation comment support

No

Yes

(/ ** … * /)

Virtual Keyword

C ++ virtual keywoard का समर्थन करता है ताकि हम यह तय कर सकें कि किसी function को override किया जाए या नहीं।

C++ supports virtual keyword so that we can decide whether or not to override a function.

जावा में कोई virtual keyword नहीं है। हम डिफ़ॉल्ट रूप से सभी non-static methods को override कर सकते हैं। दूसरे शब्दों में, non-static methods डिफ़ॉल्ट रूप से virtual हैं।

Java has no virtual keyword. We can override all non-static methods by default. In other words, non-static methods are virtual by default.

Object-oriented

यह Single Root Hierarchy को सपोर्ट नहीं करती है।

In C++ single root hierarchy not possible.

यह एक single root hierarchy है क्योंकि सब कुछ java.lang.Object से प्राप्त होता है।

It is a single root hierarchy as everything gets derived from java.lang.Object.

 

 


 

(IV) What is the scope resolution operator? Give an Example.

The scope resolution operator (::) is used for several reasons. 

·         If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable.

·         It is also used to define a function outside the class and used to access the static variables of class.

It takes the following form:

:: variable-name

 

Example

Output

#include <iostream>

using namespace std;

char a = 'm';

static int b = 50;

 

int main() {

   char a = 's';

 

   cout << "The static variable : "<< ::b;

   cout << "\nThe local variable : " << a;

   cout << "\nThe global variable : " << ::a;

 

   return 0;

}

 

The static variable : 50
The local variable : s
The global variable : m

 

 


 

(V)Explain the term 'Encapsulation', in context of object oriented programming?

Answer:-

“Data और functions को एक साथ एक class के अंदर रखना encapsulation कहलाता है” Encapsulation के कारण हम data को direct access नहीं कर सकते और इसे class के functions के द्वारा ही access किया जा सकता है. यह OOPs (object-oriented programming) का सबसे महत्वपूर्ण feature है। Encapsulation की वजह से data सुरक्षित रहता है और उसका गलत इस्तेमाल नहीं होता।

इसके द्वारा data hiding और data abstraction को प्राप्त किया जा सकता है।

 

#include <iostream.h>

using namespace std;

 

class A{

    int a, b;

 

public:

    A(){

    cout<<"Enter two Numbers"<<endl;

    cin>>a>>b;

}

    void display(){

    cout<<"Addition of "<<a<<" and "<<b<<" is "<<a+b<<endl;

    cout<<"Subtraction of "<<a<<" and "<<b<<" is "<<a-b<<endl;

    cout<<"Multiplication of "<<a<<" and "<<b<<" is "<<a*b<<endl;

    cout<<"Division "<<a<<" and "<<b<<" is "<<a/b<<endl;

}

};

int main(){

 

A obj;

obj.display();

return 0;

}

Enter two Numbers

12

6

Addition of 12 and 6 is 18

Subtraction of 12 and 6 is 6

Multiplication of 12 and 6 is 72

Division 12 and 6 is 2

Benefits of encapsulation include:

·         Encapsulation protects an object from unwanted access by clients.

·         Encapsulation allows access to a level without revealing the complex details below that level.

·         It reduces human errors.

·         Simplifies the maintenance of the application.

·         Makes the application easier to understand.

 


(VI) What is the use of This Pointer?

Solution:

In C++ programming, this is a keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++.

  • It can be used to pass current object as a parameter to another method.
  • It can be used to refer current class instance variable.
  • It can be used to declare indexers.

1.       #include <iostream>  

2.       using namespace std;  

3.       class Employee {  

4.          public:  

5.              int id; //data member (also instance variable)      

6.              string name; //data member(also instance variable)  

7.              float salary;  

8.              Employee(int id, string name, float salary)    

9.               {    

10.                 this->id = id;    

11.                this->name = name;    

12.                this->salary = salary;   

13.            }    

14.           void display()    

15.            {    

16.                cout<<id<<"  "<<name<<"  "<<salary<<endl;    

17.            }    

18.    };  

19.    int main(void) {  

20.        Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee   

21.        Employee e2=Employee(102, "Nakul", 59000); //creating an object of Employee  

22.        e1.display();    

23.        e2.display();    

24.        return 0;  

25.    }  

 

101  Sonoo  890000

102  Nakul  59000

 


 

(VII) How do you initialize an array?

Ans:-

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

 

Single Dimensional Array Initialization

The process of assigning values to the array elements is called array initialization. Once an array is declared, its elements must be initialized before they can be used in the program. If they are not properly initialized the program produces unexpected results. Let us understand how single dimensional arrays are initialized using examples.

datatype array-name[size];


Example 3 :

int arr[5];

arr[0]=100;

arr[1]=200;

arr[2]=300;

arr[3]=400;

arr[4]=500;

 

int arr[5] = {100,200,300,400,500};

int arr[] = {100,200,300,400,500,600,700,800};

 

Multidimensional Array Initialization

A multidimensional array is an array which consists of multiple arrays. Let us understand how multidimensional arrays (two-dimensional in this case) are initialized in examples 7, 8 and 9.

type array-Name [ x ][ y ];
int a[2][3] = {  
   {0, 2, 1} ,   /*  row at index 0 */
   {4, 3, 7} ,   /*  row at index 1 */
   };

 

Viii. Why is the main function declared with Public, Static, Void in Java?

Solution :

Public — we need to make the main method public because it will be called by code outside of its class when the program is started. 

Static —we need to make the main method static because Java Virtual Machine (JVM) will call it to start the program even before any objects of the class are created. 

Void — the void keyword tells the compiler that the main method will not return any value.

सार्वजनिक - जब प्रोग्राम चालू हो जाता है तब मैन मैथड को क्‍लास से बाहर कॉल करने के लिए Public  की वर्ड का इस्‍तेमाल किया जाता है।

स्टेटिक - आब्‍जेक्‍ट बनाने से पहले ही वर्चुअल मशीन (JVM)  को प्रोग्राम स्‍टार्ट करने के लिए मैन मैथड की आवश्‍यकता होती है इसलिए इसे Static डिक्‍लेयर करना पडता है।

शून्य - Void कीवर्ड कंपाइलर को बताता है कि मैन मैथड कोई वैल्‍यू/मान नहीं लौटाएगा।

 


No comments:

Post a Comment