Question 1
Processes P
1
and P
2
use critical_flag in the following routine to achieve mutual exclusion. Assume that critical_flag is initialized to FALSE in the main program.
get_exclusive_access ( ) { if (critical _flag == FALSE) { critical_flag = TRUE ; critical_region () ; critical_flag = FALSE; } }
Consider the following statements.
i. It is possible for both P
1
and P
2
to access critical_region concurrently.
ii. This may lead to a deadlock.
Which of the following holds?
(i) is false and (ii) is true
Both (i) and (ii) are false
(i) is true and (ii) is false
Both (i) and (ii) are true
Question 2
Consider the following program:
#include <iostream>
#include <cctype>
#include <stack>
#define EOF -1
std::stack<int> s;
void push(int value) { s.push(value); } /* push the argument on the stack */
int pop(void) { if (s.empty()) return EOF; int value = s.top(); s.pop(); return value; } /* pop the top of the stack */
void flagError() { std::cerr << "Error!" << std::endl; exit(1); }
int main() {
int c, m, n, r;
while ((c = std::cin.get()) != EOF) {
if (isdigit(c))
push(c - '0');
else if ((c == '+') || (c == '*')) {
m = pop();
n = pop();
r = (c == '+') ? n + m : n * m;
push(r);
} else if (c != ' ')
flagError();
}
std::cout << pop() << std::endl;
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define EOF -1
#define MAX_STACK_SIZE 100
int s[MAX_STACK_SIZE];
int top = -1;
void push(int value) { if (top < MAX_STACK_SIZE - 1) s[++top] = value; } /* push the argument on the stack */
int pop(void) { if (top == -1) return EOF; return s[top--]; } /* pop the top of the stack */
void flagError() { fprintf(stderr, "Error!\n"); exit(1); }
int main() {
int c, m, n, r;
while ((c = getchar()) != EOF) {
if (isdigit(c))
push(c - '0');
else if ((c == '+') || (c == '*')) {
m = pop();
n = pop();
r = (c == '+') ? n + m : n * m;
push(r);
} else if (c != ' ')
flagError();
}
printf("%d\n", pop());
}
import java.util.*;
public class Main {
static Stack<Integer> stack = new Stack<>();
static void push(int value) { stack.push(value); } /* push the argument on the stack */
static int pop() { return stack.isEmpty() ? -1 : stack.pop(); } /* pop the top of the stack */
static void flagError() { System.err.println("Error!"); System.exit(1); }
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int c, m, n, r;
while ((c = scanner.next().charAt(0)) != -1) {
if (Character.isDigit(c))
push(c - '0');
else if (c == '+' || c == '*') {
m = pop();
n = pop();
r = (c == '+') ? n + m : n * m;
push(r);
} else if (c != ' ')
flagError();
}
System.out.println(pop());
}
}
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item) # push the argument on the stack
def pop(self):
return self.items.pop() if self.items else -1 # pop the top of the stack
def flagError():
print("Error!")
exit(1)
s = Stack()
while True:
try:
c = input()
if c == '':
break
c = ord(c)
if c >= ord('0') and c <= ord('9'):
s.push(c - ord('0'))
elif c == ord('+') or c == ord('*'):
m = s.pop()
n = s.pop()
r = n + m if c == ord('+') else n * m
s.push(r)
elif c != ord(' '):
flagError()
except EOFError:
break
print(s.pop())
class Stack {
constructor() {
this.items = [];
}
push(item) { this.items.push(item); } /* push the argument on the stack */
pop() { return this.items.length === 0 ? -1 : this.items.pop(); } /* pop the top of the stack */
}
function flagError() { console.error('Error!'); process.exit(1); }
const stack = new Stack();
process.stdin.on('data', function (data) {
const input = data.toString();
for (let c of input) {
if (!isNaN(c)) {
stack.push(parseInt(c));
} else if (c === '+' || c === '*') {
const m = stack.pop();
const n = stack.pop();
const r = (c === '+') ? n + m : n * m;
stack.push(r);
} else if (c !== ' ') {
flagError();
}
}
});
process.stdin.on('end', function () {
console.log(stack.pop());
});
What is the output of the program for the following input ? 5 2 * 3 3 2 + * +
15
25
30
150
Question 3
Consider the C program given below :
#include <stdio.h>
int main () {
int sum = 0, maxsum = 0, i, n = 6;
int a [] = {2, -2, -1, 3, 4, 2};
for (i = 0; i < n; i++) {
if (i == 0 || a [i] < 0 || a [i] < a [i - 1]) {
if (sum > maxsum) maxsum = sum;
sum = (a [i] > 0) ? a [i] : 0;
}
else sum += a [i];
}
if (sum > maxsum) maxsum = sum ;
printf ("%d\\n", maxsum);
}
What is the value printed out when this program is executed?
9
8
7
6
Question 4
The address sequence generated by tracing a particular program executing in a pure demand paging system with 100 bytes per page is
0100, 0200, 0430, 0499, 0510, 0530, 0560, 0120, 0220, 0240, 0260, 0320, 0410.
Suppose that the memory can store only one page and if x is the address which causes a page fault then the bytes from addresses x to x + 99 are loaded on to the memory.
How many page faults will occur ?
0
4
7
8
Question 6
The function f is defined as follows:
int f(int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n / 2);
else return f(3 * n - 1);
}
int f (int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n/2);
else return f(3n - 1);
}
int f(int n) {
if (n <= 1) return 1;
else if (n % 2 == 0) return f(n / 2);
else return f(3 * n - 1);
}
def f(n):
if n <= 1:
return 1
elif n % 2 == 0:
return f(n // 2)
else:
return f(3 * n - 1)
function f(n) {
if (n <= 1) return 1;
else if (n % 2 === 0) return f(n / 2);
else return f(3 * n - 1);
}
Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.
1. The function f terminates for finitely many different values of n ≥ 1.
ii. The function f terminates for infinitely many different values of n ≥ 1.
iii. The function f does not terminate for finitely many different values of n ≥ 1.
iv. The function f does not terminate for infinitely many different values of n ≥ 1.
Which one of the following options is true of the above?
(i) and (iii)
(i) and (iv)
(ii) and (iii)
(ii) and (iv)
Question 7
Consider a weighted undirected graph with positive edge weights and let uv be an edge in the graph. It is known that the shortest path from the source vertex s to u has weight 53 and the shortest path from s to v has weight 65. Which one of the following statements is always true?
weight (u, v) < 12
weight (u, v) ≤ 12
weight (u, v) > 12
weight (u, v) ≥ 12
Question 8
Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or call by value methods of parameter passing.
int i ;
program main ()
{
int j = 60;
i = 50;
call f (i, j);
print i, j;
}
procedure f (x, y)
{
i = 100;
x = 10;
y = y + i ;
}
Which one of the following options represents the correct output of the program for the two parameter passing mechanisms?
Call by value : i = 70, j = 10; Call by reference : i = 60, j = 70
Call by value : i = 50, j = 60; Call by reference : i = 50, j = 70
Call by value : i = 10, j = 70; Call by reference : i = 100, j = 60
Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70
Question 9
Which of the following input sequences for a cross-coupled R-S flip-flop realized with two NAND gates may lead to an oscillation ?
11, 00
01, 10
10, 01
00, 11
Question 10
The following circuit implements a two-input AND gate using two 2-1 multiplexers. What are the values of X1, X2, X3 ?
X1=b, X2=0, X3=a
X1=b, X2=1, X3=b
X1=a, X2=b, X3=1
X1=a, X2=0, X3=b
There are 82 questions to complete.