# Python quiz questions

• 1.

Three concurrent processes X, Y, and Z execute three different code segments that access and update certain shared variables. Process X executes the P operation (i.e., wait) on semaphores a, b and c; process Y executes the P operation on semaphores b, c and d; process Z executes the P operation on semaphores c, d, and a before entering the respective code segments. After completing the execution of its code segment, each process invokes the V operation (i.e., signal) on its three semaphores. All semaphores are binary semaphores initialized to one. Which one of the following represents a deadlock-free order of invoking the P operations by the processes?

1. X: P(a)P(b)P(c) Y: P(b)P(c)P(d) Z: P(c)P(d)P(a)

2. X: P(b)P(a)P(c) Y: P(b)P(c)P(d) Z: P(a)P(c)P(d)

3. X: P(b)P(a)P(c) Y: P(c)P(b)P(d) Z: P(a)P(c)P(d)

4. X: P(a)P(b)P(c) Y: P(c)P(b)P(d) Z: P(c)P(d)P(a)

• 2.

A certain computation generates two arrays a and b such that a[i]=f(i) for 0 ≤ i < n and b[i]=g(a[i]) for 0 ≤ i < n. Suppose this computation is decomposed into two concurrent processes X and Y such that X computes the array a and Y computes the array b. The processes employ two binary semaphores R and S, both initialized to zero. The array a is shared by the two processes. The structures of the processes are shown below.

``````Process X:                         Process Y:
private i;                         private i;
for (i=0; i < n; i++) {            for (i=0; i < n; i++) {
a[i] = f(i);                       EntryY(R, S);
ExitX(R, S);                       b[i]=g(a[i]);
}                                 }``````

Which one of the following represents the CORRECT implementations of ExitX and EntryY?

(A)

``````ExitX(R, S) {
P(R);
V(S);
}

EntryY (R, S) {
P(S);
V(R);
}``````

(B)

``````ExitX(R, S) {
V(R);
V(S);
}

EntryY(R, S) {
P(R);
P(S);
}``````

(C)

``````ExitX(R, S) {
P(S);
V(R);
}
EntryY(R, S) {
V(S);
P(R);
}``````

(D)

``````ExitX(R, S) {
V(R);
P(S);
}
EntryY(R, S) {
V(S);
P(R);
}``````

1. A

2. B

3. C

4. D

• 3.

A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution? (GATE CS 2013)

1. -2

2. -1

3. 1

4. 2

• 4.

A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory, and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e., wait) on a counting semaphore S and invokes the V operation (i.e., signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution? (GATE CS 2013)

1. -2

2. -1

3. 1

4. 2

• 5.

Three concurrent processes X, Y, and Z execute three different code segments that access and update certain shared variables. Process X executes the P operation (i.e., wait) on semaphores a, b and c; process Y executes the P operation on semaphores b, c and d; process Z executes the P operation on semaphores c, d, and a before entering the respective code segments. After completing the execution of its code segment, each process invokes the V operation (i.e., signal) on its three semaphores. All semaphores are binary semaphores initialized to one. Which one of the following represents a deadlockfree order of invoking the P operations by the processes? (GATE CS 2013).

1. X: P(a)P(b)P(c) Y:P(b)P(c)P(d) Z:P(c)P(d)P(a)

2. X: P(b)P(a)P(c) Y:P(b)P(c)P(d) Z:P(a)P(c)P(d)

3. X: P(b)P(a)P(c) Y:P(c)P(b)P(d) Z:P(a)P(c)P(d)

4. X: P(a)P(b)P(c) Y:P(c)P(b)P(d) Z:P(c)P(d)P(a)

• 6.

The atomic fetch-and-set x, y instruction unconditionally sets the memory location x to 1 and fetches the old value of x in y without allowing any intervening access to the memory location x. consider the following implementation of P and V functions on a binary semaphore .

``````void P (binary_semaphore *s) {
unsigned y;
unsigned *x = &(s->value);
do {
fetch-and-set x, y;
} while (y);
}

void V (binary_semaphore *s) {
S->value = 0;
}``````

Which one of the following is true?

1. The implementation may not work if context switching is disabled in P.

2. Instead of using fetch-and-set, a pair of normal load/store can be used

3. The implementation of V is wrong

4. The code does not implement a binary semaphore

• 7.

Consider the following code fragment:

``````if (fork() == 0)
{ a = a + 5; printf("%d,%d\n", a, &a); }
else { a = a –5; printf("%d, %d\n", a, &a); } ``````

Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?

1. u = x + 10 and v = y

2. u = x + 10 and v != y

3. u + 10 = x and v = y

4. u + 10 = x and v != y

• 8.

What does the below code intended to do?

```def a(b, c, d):
pass```
1. defines a set and initializes it.

2. defines a function, which does nothing.

3. defines an empty class.

4. defines a method, which passes its parameters through.

• 9.

Which function does in-place reversal of objects in a list?

1. list.pop(obj=list[-1])

2. list.remove(obj)

3. list.sort([func])

4. list.reverse()

• 10.

What gets printed as the output of the below code?

```x = True
y = False
z = False
if not x or y:
print 1
elif not x or not y and z:
print 2
elif not x or y or not y and x:
print 3
else:
print 4```
1. 1

2. 4

3. 2

4. 3

• 11.

What is the output of the following Python code?

```tinytuple = (123, 'techbeamers')
print tinytuple * 2```
1. (123, 'techbeamers') * 2

2. (123, 'techbeamers', 123, 'techbeamers')

3. None of the above

4. Error

• 12.

What would be the result of the following expression in Python?

`print 3/5`
1. 0.6

2. 0

3. 6/10

4. None of the above

• 13.

What is the output of the below Python code?

```a = [1,2,3,None,(),[],]
print len(a)```
1. 5

2. 6

3. 4

4. syntax error

• 14.

If the following statement is run in Python, what will be the result?

`print abc`
1. =abc

2. 'abc'

3. NameError: name 'abc' is not defined

4. abc

• 15.

What is the output of the following code?

```def f():
pass

print type(f())```
1. <type 'function'>

2. <type 'type'>

3. <type 'NoneType'>

4. <type 'tuple'>

• 16.

What is the output of following code?

```list = [ 'Tech', 404, 3.03, 'Beamers', 33.3 ]
print list[1:3]```
1. [ 'Tech', 404, 3.03, 'Beamers', 33.3 ]

2. [404, 3.03]

3. ['Tech', 'Beamers']

4. None of the above

• 17.

If the following code is run in Python, what would be the result?

`num = '5'*'5'`
1. 333

2. 27

3. 9

4. TypeError: can't multiply sequence by non-int of type 'str'

• 18.

Which of the following functions print the output to the console?

1. console.log

2. print

3. output

4. echo

• 19.

Is the following statement correct?

There are two types of numbers in Python: integers and assigned values.

1. True

2. False

• 20.

What is the output of the following code?

```import re
sentence = 'Learn Python Programming'
test = re.match(r'(.*) (.*?) (.*)', sentence)
print(test.group())```
1. (‘Learn’, ‘Programming’)

2. (Learn, Python, Programming)

3. Learn Python Programming

4. ‘Learn Python Programming’

© 2017 QuizBucket.org