PPL - UNIT-1 Question Bank
PPL - UNIT-1 Question Bank
For example:
public class MyClass {
// class members and methods
}
Q3 Examine the necessary attributes to compute the area of a triangle L3
given its base and height.
The necessary attributes to compute the area of a triangle given its base
and height are:
1. Base: The length of the base of the triangle, which is a straight line
segment that forms the bottom of the triangle.
2. Height: The perpendicular distance from the base to the opposite vertex
(or apex) of the triangle. The height is always measured at a right angle to
the base.
These two attributes, the base and the height, are sufficient to calculate
the area of a triangle using the formula:
Static Scoping:
Python: Python does not have explicit notions of L values and R values.
In Python, the distinction between L values and R values is blurred, as
variables hold references to objects. Assigning a value to a variable is
similar to creating a name or reference to an object. Python variables can
point to any valid object, making the concept of L values and R values
less crucial.
Example:
int x = 5; // x is an l-value and can be assigned a new value
int y = x; // x is an r-value in this case as it is used to initialize y
2. L-values can be taken the address of, whereas r-values cannot. Taking
the address of an object allows you to access and modify its value
indirectly.
Example:
int x = 5;
int* ptr = &x; // valid as &x takes the address of x (l-value)
Example:
void foo(int& x) {
// do something with x
}
int a = 5;
foo(a); // valid as a is an l-value
foo(5); // invalid as 5 is an r-value
These are some of the restrictions, but the specific rules may vary
depending on the programming language you are using.
Q9. What are the different storage allocation strategies used in
programming?
These are some commonly used storage allocation strategies, and the
choice of strategy depends on the programming language, application
requirements, and performance considerations.
Q10. Compare the different translation models in programming? L2
Q13. Discuss the two way selection (if, if-else, nested if-else, cascaded if else) L2
in C language with syntax.
Two-way Selection in C:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
Q16. Discuss type checking? Differentiate between static and dynamic type L2
checking and give their relative advantages.
if x * y > 30 || z == 0:
z=x+y
else:
z=x–y
Examine the final value of z after executing this code snippet.
Final Value of z:
x=5
y = 10
z=0
if x * y > 30 || z == 0:
z = x + y, since x * y > 30 is true
Final value of z is 5 + 10 = 15.
Q19. Consider the following BNF grammar for a simple arithmetic expression L3
language:
<expr> ::= <term> | <expr> + <term> | <expr> - <term>
<term> ::= <factor> | <term> * <factor> | <term> / <factor>
<factor> ::= <number> | ( <expr> )
<number> ::= <digit> | <digit> <number>
<digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Using the given grammar, Construct the parse the following
arithmetic expression: 5 * (3 + 7) – 2
To parse the given arithmetic expression using the provided BNF
grammar, we need to break down the expression according to the
grammar rules and apply them recursively. Let's go through the steps:
Given expression: 5 * (3 + 7) - 2
Q20. Discuss the scope and lifetime of variables. Illustrate when they L2
would coincide and when they don’t.
The scope of a variable refers to the portion of the program in which the
variable can be accessed or used. The lifetime of a variable refers to the
duration during which the variable exists in the memory.
Variables can coincide in scope and lifetime when they are declared in the
same block of code, such as within a function. In this case, the variables
have the same scope (the function) and lifetime (the duration of the
function execution).
Variables may not coincide in scope and lifetime when they are declared
in different blocks of code such as one being a global variable and the
other being a local variable in a function. In this case, the global variable
has a larger scope and longer lifetime compared to the local variable.