0% found this document useful (0 votes)
3 views9 pages

Matrimony Assignment 2

Some useful documents

Uploaded by

Pooja Prakash
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)
3 views9 pages

Matrimony Assignment 2

Some useful documents

Uploaded by

Pooja Prakash
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/ 9

ASSIGNMENT-2

1) What is a transpiler? Can you name a popular programming


language that uses a transpiler?

Transpilers, or source-to-source compilers, are


tools that read source code written in one programming language,
and produce the equivalent code in another language.Suppose
you've written a program in one language but wish to convert this
to another language, then you would invoke what's called
a transpiler. The programming language at the input to the
transpiler may be called the source language whereas the
language at the output may be called the target language. A
transpiler is sometimes called a source-to-source compiler. One of
the most famous transpiler in the javascript world is Babel.

2) What are the executable container formats used in Windows 11 and


MacOS?
Windows 11
(i)exe: This is the most common executable file format
for Windows applications. It's a native format that contains the
code and data necessary for the application to run.
(ii)msi: This is a Windows Installer package file. It's used
to install applications on Windows systems. It can contain multiple
files, including .exe files, as well as registry settings and other
information needed for installation.
(iii)appx: This is a package file format for Universal
Windows Platform (UWP) applications. UWP apps are designed to
run on a variety of Windows devices, including PCs, tablets, and
phones.
(iv)wsb: This is a Windows Scripting Host file. It's used
to run scripts written in languages like VBScript and JScript.

macOS
(i)app: This is the standard application bundle format
for macOS. It's a directory that contains the application's
executable file, along with other resources like images, libraries,
and settings.
(ii)pkg: This is a package file format for installing
applications on macOS. It's similar to the .msi format on Windows,
and it can contain multiple files and settings.
(iii)command: This is a shell script file for macOS. It's
used to run commands in the Terminal.

3) Do you know of any notation used to describe the syntax of


programming languages? Try to express an email address in one of
these notations

There are several notations used to describe the syntax


of programming languages. One of the most common is Backus-
Naur Form (BNF).BNF uses a set of rules to define how different
parts of a language can be combined to form valid expressions.
Here's a simplified example of how we could express an email
address in BNF:

<email> ::= <local-part>@<domain>


<local-part> ::= <letter> | <local-part><letter>

<domain> ::= <subdomain>.<domain> | <top-level-domain>

<subdomain> ::= <letter> | <subdomain><letter> |


<subdomain><digit>
<top-level-domain> ::= com | org | net | ...

<letter> ::= a | b | c | ... | z | A | B | C | ... | Z


<digit> ::= 0 | 1 | 2 | ... | 9
<special-char> ::= ! | # | $ | % | & | ...

4) Do you think a program without functions will be faster than a


program for the same logic implemented with functions? Why?

No, a program without functions is generally not


going to be significantly faster than the same logic implemented
with functions. While technically there might be a slight
performance gain due to the minimal overhead of function calls, in
most cases, the difference would be negligible and not worth
sacrificing code readability and maintainability by avoiding
functions altogether.
Why functions don't significantly impact speed:
(i)Modern compilers optimize function calls:
Most modern compilers are very good at optimizing
function calls, minimizing the overhead of jumping to a different
part of the code.
(ii)Code reuse and modularity are more important:
The primary benefit of using functions is to break
down complex logic into smaller, reusable units, which makes code
easier to understand, debug, and maintain.
(iii)Extremely time-critical code:
In very specific scenarios where every microsecond
counts, and a program is heavily reliant on a small number of
operations that are executed millions of times, the overhead of
function calls might become noticeable.
(iv)Poorly optimized code:
If a function is not properly optimized, the overhead
of calling it might become more significant.
5) Can errors in call stack or stack frame lead to program errors like
wrong functionality or program crashes?
Yes, errors in the call stack or stack frame can
definitely lead to program errors like incorrect functionality or
crashes, most notably through "stack overflow" errors where a
program attempts to use more memory on the stack than is
available, often caused by deep or infinite recursion, resulting in a
program crash. When a program tries to add too many function
calls to the call stack, exceeding its allocated memory, it leads to a
stack overflow error, usually causing the program to crash.

6) Do you think security related vulnerabilities could be associated


with call stacks? Can you think of an example for this?
Yes, call stacks can be associated with security
vulnerabilities. Here is how:
Stack overflow
A stack overflow occurs when the amount of data in a stack
exceeds the amount of space reserved for it. This can happen when
a user's input is longer than the reserved space, and the program
doesn't verify that the input will fit. When a stack overflows, the
excess data can corrupt other variables, change variable values, and
overwrite return addresses. This can expose security vulnerabilities
that hackers can exploit, such as altering a password or deleting a
configuration file.
Stack-based buffer overflow
A stack-based buffer overflow vulnerability can be exploited by a
threat actor to craft a malicious input that triggers excessive
memory allocation. This malicious input can also deliver a
concealed payload of malicious code.
Using the same stack for data and procedure calls,using the same
stack for both data and procedure calls can introduce serious
security bugs into a program. Programmers need to be aware of
these security implications to avoid such pitfalls.
7) How will the set up of the call stack work for a recursive function
call?
When a recursive function is called, each
recursive call adds a new "frame" to the call stack, essentially
creating a stack of function calls where the most recent call is at
the top; as the function reaches its base case and starts to unwind,
each frame is popped off the stack, allowing the previous call to
resume execution, effectively working on a Last-In-First-Out
(LIFO) principle.

8) Can a call stack grow indefinitely? What happens if a function is


called recursively too many times?
Yes, a call stack can grow indefinitely if a
function is called recursively too many times, resulting in a stack
overflow error. To be exact, a stack is a collection of elements that
follows a last in, first out (LIFO) order. When a function is called
recursively, the stack grows with each call. If the recursion is too
deep, the stack can run out of space and crash the program. This is
known as a stack overflow.
Example
In JavaScript, a "too much recursion" or "Maximum call stack size
exceeded" exception occurs when a function is called too many
times or is missing a base case.

9) What does it mean to say that functions or expressions can have


“side effects”?
In programming, a function or expression is
said to have "side effects" if it modifies some state or interacts with
the outside world beyond returning a value. This can include actions
such as:
Modifying Global Variables: Changing the value of a variable that is
outside the function's local scope.
Altering Data Structures: Changing the contents of arrays, lists, or
other data structures.
Input/Output Operations: Performing actions such as printing to the
console, reading from or writing to a file, or sending data over a
network.
Exception Throwing: Raising errors or exceptions that can affect the
flow of the program.

10) How can one stop the execution of a loop before it is completed?
Illustrate the difference between ‘break’ and ‘continue’ with an example.
To stop the execution of a loop before it is
completed, one can use the break statement. The break statement
terminates the loop entirely and transfers control to the code that
follows the loop. On the other hand, the continue statement skips the
current iteration of the loop and proceeds with the next iteration
without terminating the loop.
Example:
# Using 'break'
print("Using 'break':")
for i in range(1, 6):
if i == 4:
print(f"Loop breaks at i = {i}")
break # Exit the loop when i equals 4
print(f"i = {i}")
# Using 'continue'
print("\nUsing 'continue':")
for i in range(1, 6):
if i == 4:
print(f"Skipping i = {i}")
continue # Skip the rest of the loop when i equals 4
print(f"i = {i}")
Output:
Using 'break':
i=1
i=2
i=3
Loop breaks at i = 4
Using 'continue':
i=1
i=2
i=3
Skipping i = 4
i=5

11)Write the pseudo code to find the sum of the cubes of the first 9
positive integers.
PSEUDO CODE:
BEGIN
Initialize SUM as 0
FOR i FROM 1 TO 9 DO
Compute CUBE = i * i * i
Add CUBE to SUM
END FOR
OUTPUT "The sum of the cubes of the first 9 positive
integers is:", SUM
END
Explanation:
Initialization: Start with SUM = 0 to store the cumulative total.
Loop: Use a FOR loop to iterate through integers from 1 to 9.
Cube Calculation: Compute the cube of the current integer, i.
Accumulate: Add the cube to the running total, SUM.
Output: After the loop completes, display the result.

12) Write the pseudo code to check if a given 4-digit square number will
yield another square number by adding 1 to each digit. For example, if
2025 is the input, adding 1 to each digit yields 3136.
BEGIN
INPUT number (4-digit square number)
IF number is not a 4-digit number OR number is not a perfect square
THEN
OUTPUT "Invalid input"
EXIT
END IF
Convert number to a string representation for digit manipulation
Initialize new_number as an empty string
FOR each digit in the string representation of number DO
Add 1 to the digit
Append the result to new_number
END FOR
Convert new_number back to an integer
Compute square_root = √(new_number)
IF square_root * square_root = new_number THEN
OUTPUT "New number", new_number, "is a perfect square"
ELSE
OUTPUT "New number", new_number, "is NOT a perfect square"
END IF
END
For input 2025:
Add 1 to each digit: 2025 → 3136
Check if 3136 is a perfect square:
√3136 = 56
56² = 3136 (it's a perfect square)
Output: "New number 3136 is a perfect square."

You might also like