0% found this document useful (0 votes)
26 views6 pages

PPL Mini Test - Solution

Uploaded by

Arpita Bhargava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views6 pages

PPL Mini Test - Solution

Uploaded by

Arpita Bhargava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PPL Mini test

1. What is a programming language. Discuss briefly the major features of a programming language.
2. Explain the importance of grammar in the development of programming language. Through examples
illustrate the use of context free grammar and Backus Naur form of grammar.
3. Describe compiler, interpreter and hybrid interpretation process with reference to virtual machines in them.
Also explain the significance of Just-in -time compilation method.
4. Explain the following concepts of Object Oriented Programming Language. Support your answers using
C++ programs.
a) Inheritance.
b) Polymorphism.
c) Encapsulation.
Solution to mini test
1. A programming language is a system of notation for writing computer programs. Programming
languages are described in terms of their syntax (form) and semantics (meaning), usually defined
by a formal language. Languages usually provide features such as a type system, variables and
mechanisms for error handling. An implementation of a programming language in the form of
a compiler or interpreter allows programs to be executed, either directly or by producing what's
known in programming as an executable. Computer architecture has strongly influenced the design of
programming languages, with the most common type (imperative languages—which implement
operations in a specified order) developed to perform well on the popular von Neumann architecture.
While early programming languages were closely tied to the hardware, over time they have
developed more abstraction to hide implementation details for greater simplicity.
S. Language
Specific Features Examples
No. Paradigm
Java,
Procedural They follow a step-wise/sequential format of writing commands
1. Pascal,
languages and executing them.
BASIC, etc
This refers to those languages wherein a program is written
Functional Scala,
based on mathematical functions and they are useful in
2. programming Haskell, F#,
performing computation. There are two types of these languages-
languages etc
pure functional languages and impure functional languages.
Object- The languages that consider a program as a set of objects are
oriented referred to as object-oriented languages. In other words, in such Python,
3.
programming languages, programs contain objects, which further contain data Ruby, C++
languages or code.
The commands/ instructions are written for and interpreted in Python,
A scripting
4 the runtime environment. There is no need to compile these Perl, PHP,
language
languages since they are interpreted Bash, etc
Prolong,
Logic In such languages, programs are written as a set of logical
5 Alma-0,
languages relations instead of rigid/ technical commands or instructions.
Absys, etc.
These pertain to the user side or front-end web development side CSS,
Front-end
6 and used to create what the users see in their browsers, i.e. JavaScript,
languages
images, text, colors, etc. React, etc
Back-end programming languages are used in the back-end PHP,
Back-end development end of a website, app, program, etc. These JavaScript,
7
languages languages are used to address the server-side requests that PHP,
ultimately produce what the user sees on the front end Python, etc
8 Compiled a compiler is used to convert programming language code into C, C+
languages machine language which the processor then implements/ +, Rust, etc.
executes.
These are those languages that skip the compiler step and instead Python,
Interpreted
9 are interpreted at the time of execution/ at runtime, by an JavaScript,
languages
interpreter (think back to the explanation of scripting languages). PHP, etc

2. Grammar lets us transform a program, which is normally represented as a linear sequence


of ASCII characters, into a syntax tree. Only programs that are syntactically valid can be transformed
in this way. This tree will be the main data-structure that a compiler or interpreter uses to process the
program. By traversing this tree the compiler can produce machine code, or can type check the
program, for instance. And by traversing this very tree the interpreter can simulate the execution of
the program. There are many different ways to describe the semantics of a programming language;
however, after decades of study, there is mostly one technology to describe its syntax. We call this
formalism the context free grammar. There are many different ways to describe the semantics of a
programming language; however, after decades of study, there is mostly one technology to describe
its syntax. We call this formalism the context free grammars.
3. Virtual machines (VMs) provide an intermediate stage for the compilation of programming
languages. VMs are machines because they permit a step-by-step execution of programs. VMs are
virtual (abstract) because typically they are not implemented in hardware and they omit many details
of real (hardware) machines

4.
VMs are tailored to the particular operations required to implement a particular (class of) source
language(s)
5.
Backus-Naur Form: The main notation used to represent grammars is the Backus-Naur Form, or
BNF for short. This notation, invented by John Backus and further improved by Peter Naur, was first
used to describe the syntax of the Algol programming language. A BNF grammar is defined by a
four-elements tuple represented by (T, N, P, S). The meaning of these elements is as follows:
T is a set of Tokens form the vocabulary of the language and are the smallest units of
tokens. syntax. These elements are the symbols that programmers see when they
are typing their code
N is a set of Nonterminals are not part of the language but they help to determine the
nonterminals. structure of the derivation trees that can be derived from the grammar.
Usually we enclose these symbols in angle brackets, to distinguish them
from the terminals.
P :Set of Each production is composed of a left-hand side, a separator and a right-
production rules hand side, e.g., <non-terminal> := <expr1> ... <exprN>, where ':=' is the
separator. For convenience, productions with the same left-hand side can be
abbreviated using the symbol '|'. The pipe, in this case, is used to separate
different alternatives.
S is a start Any sequence of derivations that ultimately produces a grammatically valid
symbol. program starts from this special non-terminal

6. Object Oriented programming model is an important paradigm of programming language. It is


based certain set of principles. Following are some of those principles that forms the pillars of OOPs

a) Inheritance: The capability of a class to derive properties and characteristics from another class
is called Inheritance. Inheritance is one of the most important features of Object-Oriented
Programming.
Inheritance is a feature or a process in which, new classes are created from the existing classes.
The new class created is called “derived class” or “child class” and the existing class is known as
the “base class” or “parent class”. The derived class now is said to be inherited from the base
class.
When we say derived class inherits the base class, it means, the derived class inherits all the
properties of the base class, without changing the properties of base class and may add new
features to its own. These new features in the derived class will not affect the base class. The
derived class is the specialized class for the base class.
1.Sub Class: The class that inherits properties from another class is called Subclass or Derived
Class.
2.BaseClass or Super Class: The class whose properties are inherited by a subclass is called
Base Class or Superclass.
The following C++ code discusses the illustration of base class and sub class usage for application
of concepts of inheritence.
---------------------------------------------------------------------------
// C++ program to demonstrate implementation of Inheritance
---------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
// Base class
class Parent {
public:
int id_p;
};

// Sub class inheriting from Base Class(Parent)


class Child : public Parent
{
public:
int id_c;
};

// main function
int main()
{
Child obj1;
// An object of class child has all data members and member functions
of class parent
obj1.id_c = 7;
obj1.id_p = 91;
cout << "Child id is: " << obj1.id_c << '\n';
cout << "Parent id is: " << obj1.id_p << '\n';
return 0;
}
-----------------------------------------------------------------------------------------------------------

b) Polymorphism: Polymorphism is the ability of any data to be processed in more than one
form i.e. poly means many and morphism means types. Polymorphism is one of the most
important concepts of object-oriented programming languages. The most common use of
polymorphism in object-oriented programming occurs when a parent class reference is used to
refer to a child class object. Here we will see how to represent any function in many types and
many forms. In a real-life example of polymorphism, a person at the same time can have
different roles to play in life. Like a man, at the same time is a son, a wife, an employee and a
student. So the same person has to have many features but has to implement each as per the
situation and the condition. It is the ability of an object or reference to take many forms in
different instances. For this purpose it uses the concept of function overloading, function
overriding and virtual functions. T
---------------------------------------------------------------------------
// C++ program to demonstrate implementation of Polymorphism
---------------------------------------------------------------------------
// Base class
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n";
}
};
// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n";
}
};
// Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n";
}
};
int main() {
Animal myAnimal;
Pig myPig;
Dog myDog;

myAnimal.animalSound();
myPig.animalSound();
myDog.animalSound();
return 0;
}
---------------------------------------------------------------------------
Encapsulation: Encapsulation in C++ is defined as the wrapping up of data and information in a
single unit. In Object Oriented Programming, Encapsulation is defined as binding together the data
and the functions that manipulate them.
The act of encapsulating involves combining data and the methods that manipulate it into a
single entity, referred to as a class. A class in C++ is a user-defined data type that has member
methods and data members. The member functions are methods that work on the data
members, which are variables that hold an object's state. The following C++ code illustrates
the use of C++ codes for achieving encapsulation.
------------------------------------------------------------------------------
// C++ program to demonstrate implementation of Polymorphism
------------------------------------------------------------------------------
#include <iostream>
using namespace std;
class Employee {
private:
int empId;
string empName;
float empSalary;

public:
void setEmpId(int id) {
empId = id;
}
void setEmpName(string name) {
empName = name;
}
void setEmpSalary(float salary) {
empSalary = salary;
}
int getEmpId() {
return empId;
}
string getEmpName() {
return empName;
}
float getEmpSalary() {
return empSalary;
}
};

int main() {
Employee emp;
emp.setEmpId(101);
emp.setEmpName("John Doe");
emp.setEmpSalary(5000.0);

cout << "Employee ID: " << emp.getEmpId() << endl;


cout << "Employee Name: " << emp.getEmpName() << endl;
cout << "Employee Salary: " << emp.getEmpSalary() << endl;

return 0;
}
----------------------------------------------------------------------------------------------------------------------

You might also like