You are on page 1of 8

Computer science – Assessment questions – Level 2 – Set 1

Question 1: Explain the concept of pointers in C++. Provide a program example of how pointers can be used to
manipulate data and access memory locations.

Answer :
In C++, a pointer is a variable that holds the memory address of another variable. Pointers are powerful tools that
allow you to perform operations on memory directly, giving you fine-grained control over data.

Here's an explanation of pointers using a simple program example:

#include <iostream>

using namespace std;

int main() {

int num = 10; // Declare an integer variable 'num' with value 10

int* ptr; // Declare a pointer to an integer

ptr = &num; // Assign the address of 'num' to the pointer 'ptr'

cout << "Value of num: " << num << endl;

cout << "Address of num: " << &num << endl;

cout << "Value of ptr: " << ptr << endl;

cout << "Value pointed by ptr: " << *ptr << endl;

*ptr = 20; // Change the value of 'num' through the pointer

cout << "New value of num: " << num << endl;

return 0;

Expected output :

Value of num: 10

Address of num: 0x7ffeefbff4ac

Value of ptr: 0x7ffeefbff4ac

Value pointed by ptr: 10


New value of num: 20

Explanation:

1. int num = 10;: This declares an integer variable num and assigns it the value 10.

2. int* ptr;: This declares a pointer to an integer named ptr. The asterisk (*) indicates that ptr is a pointer.

3. ptr = &num;: This assigns the address of num to the pointer ptr. The ampersand (&) operator is used to get
the address of a variable.

4. cout << "Value of num: " << num << endl;: This prints the value of num.

5. cout << "Address of num: " << &num << endl;: This prints the memory address of num.

6. cout << "Value of ptr: " << ptr << endl;: This prints the value stored in the pointer ptr, which is the address of
num.

7. cout << "Value pointed by ptr: " << *ptr << endl;: This uses the asterisk (*) operator to access the value
pointed to by ptr. This will print the value of num.

8. *ptr = 20;: This changes the value of num indirectly through the pointer ptr.

9. cout << "New value of num: " << num << endl;: This prints the new value of num.

In this example, we use a pointer to indirectly access and modify the value of num. This demonstrates how pointers
can be used to manipulate data and access memory locations directly.

Note: working with pointers requires careful handling to avoid memory errors like segmentation faults. Always
ensure that pointers are properly initialized and dereferenced.

Question 2: Explain the concept of deadlock in operating systems. Provide an example of a deadlock situation and
discuss potential strategies to prevent or resolve deadlocks.

Answer :

! Deadlock is a common issue in operating systems where two or more processes are unable to proceed because
each is waiting for another to release a resource, or more than one process is holding resources and waiting for
additional resources acquired by other processes. In a deadlock, none of the processes can make progress, and the
system essentially becomes stuck.

To understand deadlock, let's introduce some key concepts:

1. Resource: In the context of operating systems, a resource can refer to anything that a process may need to
execute, such as CPU time, memory, or I/O devices.

2. Preemptable vs. Non-preemptable Resources:

• Preemptable resources can be taken away from a process without causing any harm. For example,
CPU time can be allocated and revoked without issue.

• Non-preemptable resources cannot be taken away once granted. For example, if a process has
exclusive access to a printer, it cannot be forcibly taken away.

3. Requests and Releases: Processes request resources when they need them and release them when they are
done.
4. Hold and Wait: A process holds a resource while simultaneously waiting for another resource. This is often a
key condition for deadlock.

5. Circular Wait: A set of processes are waiting for each other in a circular chain. For example, Process A is
waiting for a resource held by Process B, and Process B is waiting for a resource held by Process A.

Example of a Deadlock:

Consider two processes, A and B, and two resources, X and Y.

1. Process A acquires resource X.

2. Process B acquires resource Y.

3. Process A now needs resource Y, but it's held by Process B.

4. Process B now needs resource X, but it's held by Process A.

In this scenario, both processes are holding a resource and waiting for the other resource to be released. This creates
a deadlock.

Strategies to Prevent or Resolve Deadlocks:

1. Prevention:

• Mutual Exclusion: Ensure that at least one resource is preemptable. This means that if a process is
holding a resource, it can be forcibly taken away if needed.

• Hold and Wait: Require processes to request all the resources they need at once, rather than
piecemeal. This way, a process won't hold resources while waiting for others.

• No Preemption: Ensure that no resources are non-preemptable. While this can be impractical for
many resources (like a printer), it's a valid strategy for certain types.

• Resource Ordering: Define a strict ordering of resource requests and require processes to request
resources in that order. This can eliminate circular waits.

2. Detection and Recovery:

• Deadlock Detection: Periodically check the system for deadlocks. If one is detected, the system can
take action to resolve it.

• Deadlock Resolution:

• Resource Preemption: If a deadlock is detected, one or more processes can have their
resources forcibly taken away. This, however, can be complex and may lead to other issues.

• Process Termination: Terminate one or more processes involved in the deadlock. This is
usually a last resort and should be done carefully.

3. Avoidance:

• Banker's Algorithm: This is a method for safely allocating resources to processes in a way that avoids
deadlocks. It requires knowing the maximum resource needs of each process in advance.

Final Answer :

Deadlock in operating systems occurs when two or more processes are stuck because each is holding a resource and
waiting for another. This creates a standstill where no progress can be made. Prevention strategies include ensuring
resources are preemptable, requiring processes to request all needed resources at once, and using resource
ordering. Detection and recovery methods involve periodically checking for deadlocks and, if found, either
preempting resources or terminating processes. The Banker's Algorithm is an avoidance strategy that allocates
resources safely. The choice of strategy depends on the system's specific requirements and characteristics.

Question 3: Create a simple HTML page with a form that includes input fields for name, email, and a submit
button. On submitting the form, display a message showing the entered information.

Answer :

Here's a simple HTML code that creates a form with input fields for name and email, along with a submit button.
When the form is submitted, it will display a message showing the entered information.

HTML :

<!DOCTYPE html>

<html>

<head>

<title>Simple Form</title>

<script>

function displayInfo() {

var name = document.getElementById("name").value;

var email = document.getElementById("email").value;

var message = "Name: " + name + "<br>Email: " + email;

document.getElementById("result").innerHTML = message;

return false; // Prevents the form from actually submitting

</script>

</head>

<body>

<form onsubmit="return displayInfo()">

<label for="name">Name:</label>

<input type="text" id="name" required><br><br>

<label for="email">Email:</label>

<input type="email" id="email" required><br><br>

<input type="submit" value="Submit">

</form>

<div id="result"></div>
</body>

</html>

Expected output :

Name: John Doe

Email: johndoe@exampl.com

Step by step explanation of code :

1. <!DOCTYPE html>: This is the document type declaration and it specifies that the document is an HTML5
document.

2. <html>: This is the opening tag for the HTML document.

3. <head>: This is the head section of the HTML document. It contains meta-information about the document,
such as the title (specified by <title>), links to external resources, and scripts.

4. <title>Simple Form</title>: This sets the title of the HTML document to "Simple Form". This title is displayed
in the browser's title bar or tab.

5. <script>: This is an opening script tag. JavaScript code can be placed inside this tag.

6. function displayInfo() {...}: This is a JavaScript function named displayInfo. This function is called when the
form is submitted. It performs the following actions:

• It retrieves the values entered in the input fields with the IDs "name" and "email".

• It constructs a message containing the name and email.

• It updates the content of the <div> with the ID "result" to display the message.

• Finally, it returns false to prevent the form from actually submitting.

7. </script>: This is the closing script tag.

8. </head>: This is the closing tag for the head section.

9. <body>: This is the body section of the HTML document. It contains the visible content of the webpage.

10. <form onsubmit="return displayInfo()">: This is the opening tag for a form. The onsubmit attribute specifies
that when the form is submitted, the displayInfo() function should be called. The return statement is used to
prevent the form from actually submitting.

11. <label for="name">Name:</label>: This creates a label for the input field with the ID "name". The label
displays the text "Name:".

12. <input type="text" id="name" required>: This creates an input field of type text. It has an ID of "name"
which is used to identify it in the JavaScript code. The required attribute specifies that the field must be filled
out before the form can be submitted.

13. <label for="email">Email:</label>: This creates a label for the input field with the ID "email". The label
displays the text "Email:".

14. <input type="email" id="email" required>: This creates an input field of type email. It has an ID of "email"
which is used to identify it in the JavaScript code. The required attribute specifies that the field must be filled
out before the form can be submitted.

15. <input type="submit" value="Submit">: This creates a submit button.


16. </form>: This is the closing tag for the form.

17. <div id="result"></div>: This creates a <div> element with an ID of "result". This is where the message with
the entered information will be displayed.

18. </body>: This is the closing tag for the body section.

19. </html>: This is the closing tag for the HTML document.

When you open this HTML file in a web browser, you will see a form with input fields for name and email, and a
submit button. When you fill in the fields and click "Submit", the displayInfo() function is called, which retrieves the
values, constructs a message, and displays it below the form.

Question 4: Explain the Agile software development methodology. Discuss its advantages and how it differs from
the traditional Waterfall model.

Answer :

The Agile software development methodology is an iterative and incremental approach to software development. It
prioritizes flexibility, collaboration, and customer satisfaction. Here are the key aspects of Agile:

Agile Methodology:

Iterative Development: Agile divides the project into small increments with minimal planning, and each iteration
results in a potentially shippable product increment.

Customer Collaboration: It encourages constant communication with the customer or stakeholders to gather
feedback and adapt to changing requirements.

Cross-Functional Teams: Agile promotes small, cross-functional teams that work together to accomplish the project
goals.

Adaptive Planning: Plans are flexible and can change as the project progresses based on feedback and evolving
requirements.

Time-Boxed Iterations (Sprints): Work is divided into fixed-length time frames (usually 2-4 weeks) called sprints. Each
sprint has a specific set of goals and delivers a potentially shippable product increment.

Continuous Integration and Testing: Code is integrated frequently, and automated tests are run to ensure that the
software remains functional throughout the development process.

Prioritization and Backlog: The project's requirements are listed in a prioritized backlog. The team works on the
highest priority items first.

Emphasis on Individuals and Interactions: Agile values face-to-face communication and collaboration over processes
and tools.

Advantages of Agile:

Flexibility and Adaptability: Agile allows for changes to be made even late in the development process, responding
quickly to customer feedback or changing business needs.

Faster Time-to-Market: Because the product is developed incrementally, parts of it can be released to the customer
sooner, providing value earlier.

Improved Customer Satisfaction: Regular interaction with the customer ensures that their needs are met and allows
for adjustments to be made based on their feedback.

Reduced Risk of Project Failure: By delivering increments of the product throughout the development process, Agile
reduces the risk of delivering a final product that doesn't meet the customer's needs.
Better Quality: Continuous integration and testing practices in Agile lead to a higher-quality product as bugs are
caught early and fixed promptly.

Differences from the Waterfall Model:

Sequential vs. Iterative: Waterfall follows a linear and sequential approach, where each phase must be completed
before the next begins. Agile, on the other hand, is iterative, allowing for flexibility and adaptation throughout the
development process.

Requirements and Changes: In Waterfall, requirements are fixed at the beginning, and changes are difficult and
expensive to implement. In Agile, requirements can evolve and change even late in the process.

Customer Involvement: Agile emphasizes constant customer collaboration, while Waterfall often involves the
customer primarily at the beginning and the end of the project.

Delivery Time: Waterfall typically delivers the entire project at the end of the development cycle, while Agile delivers
smaller increments of the product in regular intervals.

Overall, Agile provides a more dynamic and customer-centric approach to software development, which can be
highly beneficial in industries with rapidly changing requirements or where customer feedback is crucial.

Question 5: A network has a total bandwidth of 1 Gbps (Gigabit per second). If the network is shared among 50
users, calculate the maximum data rate (in Mbps) that each user can achieve if they are all using the network
simultaneously.

Answer :

To find the maximum data rate that each user can achieve when the network is shared among 50 users, we'll use the
concept of network bandwidth sharing.

The formula to calculate the maximum data rate per user is:

Maximum Data Rate per User (Mbps)=Total Bandwidth (Mbps)Number of UsersMaximum Data Rate per User (Mbps)
=Number of UsersTotal Bandwidth (Mbps)

Given:

Total Bandwidth = 1 Gbps = 1000 Mbps

Number of Users = 50

Plug in these values into the formula:

Maximum Data Rate per User (Mbps)=100050=20 Mbps per userMaximum Data Rate per User (Mbps)=501000
=20 Mbps per user

Each user can achieve a maximum data rate of 20 Mbps when all 50 users are using the network simultaneously.

Step by step explanation :

Step 1: Understand the Given Information

• Total Bandwidth = 1 Gbps = 1000 Mbps

• Number of Users = 50

Step 2: Recall the Formula The formula to calculate the maximum data rate per user is:

Maximum Data Rate per User (Mbps)=Total Bandwidth (Mbps)Number of UsersMaximum Data Rate per User (Mbps)
=Number of UsersTotal Bandwidth (Mbps)
Step 3: Plug in the Values Substitute the given values into the formula:

Maximum Data Rate per User (Mbps)=100050Maximum Data Rate per User (Mbps)=501000

Step 4: Perform the Division


Maximum Data Rate per User (Mbps)=20 MbpsMaximum Data Rate per User (Mbps)=20 Mbps

Step 5: Interpret the Result This means that when all 50 users are using the network simultaneously, each user can
achieve a maximum data rate of 20 Mbps.

Summary:

• We used the given values of total bandwidth (1000 Mbps) and number of users (50).

• Applied the formula for maximum data rate per user.

• Performed the division to get the result of 20 Mbps.

--------------------------------------THANK YOU------------------------------------

BY :
VALLAPUDASU GANESH

You might also like