Advertisement
udaykumar1997

l

May 2nd, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.44 KB | None | 0 0
  1. --------------------------
  2. Post Title 'IA-2 Question Bank, Solved'
  3. Post by user 'Adusumilli-Uday-Kumar'
  4. Post encryped with secure 256-bit MD5 Encryption
  5. Post accesable only VIA link
  6. Post will Expire on '21-SEP-2017'
  7. --------------------------
  8.  
  9.  
  10.  
  11. Q1.
  12.  
  13. \\
  14.  
  15.  
  16.  
  17. Q2.
  18.  
  19. \\
  20.  
  21.  
  22.  
  23. Q3.
  24.  
  25. \\
  26.  
  27.  
  28.  
  29. Q4.
  30.  
  31. \\
  32.  
  33.  
  34.  
  35. Q5.
  36.  
  37. \\
  38.  
  39.  
  40.  
  41. Q6.
  42.  
  43. \\
  44.  
  45.  
  46.  
  47. Q7.
  48.  
  49. \\
  50.  
  51.  
  52.  
  53. Q8.
  54.  
  55. \\
  56.  
  57.  
  58.  
  59. Q9.
  60.  
  61. \\
  62.  
  63.  
  64.  
  65. Q10.
  66.  
  67. \\
  68.  
  69.  
  70.  
  71. Q11.
  72.  
  73. \\
  74.  
  75.  
  76.  
  77. Q12.
  78.  
  79. \\
  80.  
  81.  
  82.  
  83. Q13.
  84.  
  85. \\
  86.  
  87.  
  88.  
  89. Q14.
  90.  
  91. The following program incorrectly implements a simple form of the producer/ consumer problem. It consists of four classes: Q, the queue that you’re trying to synchronize; Producer, the threaded object that is producing queue entries; Consumer, the threaded object that is consuming queue entries; and PC, the tiny class that creates the single Q, Producer, and Consumer.
  92.  
  93. // An incorrect implementation of a producer and consumer.
  94. class Q {
  95. int n;
  96. synchronized int get() {
  97. System.out.println("Got: " n);
  98. return n;
  99. }
  100. synchronized void put(int n) {
  101. this.n = n;
  102. System.out.println("Put: " n);
  103. }
  104. }
  105. class Producer implements Runnable {
  106. Q q;
  107. Producer(Q q) {
  108. this.q = q;
  109. new Thread(this, "Producer").start();
  110. }
  111. public void run() {
  112. int i = 0;
  113. while(true) {
  114. q.put(i );
  115. }
  116. }
  117. }
  118. class Consumer implements Runnable {
  119. Q q;
  120. Consumer(Q q) {
  121. this.q = q;
  122. new Thread(this, "Consumer").start();
  123. }
  124. public void run() {
  125. while(true) {
  126. q.get();
  127. }
  128. }
  129. }
  130. class PC {
  131. public static void main(String args[]) {
  132. Q q = new Q();
  133. new Producer(q);
  134. new Consumer(q);
  135. System.out.println("Press Control-C to stop.");
  136. }
  137. }
  138.  
  139. Although the put( ) and get( ) methods on Q are synchronized, nothing stops the producer from overrunning the consumer, nor will anything stop the consumer from consuming the same queue value twice. Thus, you get the erroneous output shown here (the exact output will vary with processor speed and task load):
  140.  
  141. Put: 1
  142. Got: 1
  143. Got: 1
  144. Got: 1
  145. Got: 1
  146. Got: 1
  147. Put: 2
  148. Put: 3
  149. Put: 4
  150. Put: 5
  151. Put: 6
  152. Put: 7
  153. Got: 7
  154.  
  155. After the producer put 1, the consumer started and got the same 1 five times in a row. Then, the producer resumed and produced 2 through 7 without letting the consumer have a chance to consume them. The proper way to write this program in Java is to use wait( ) and notify( ) to signal in
  156. both directions, as shown here:
  157.  
  158. // A correct implementation of a producer and consumer.
  159. class Q {
  160. int n;
  161. boolean valueSet = false;
  162. synchronized int get() {
  163. while(!valueSet)
  164. try {
  165. wait();
  166. } catch(InterruptedException e) {
  167. System.out.println("InterruptedException caught");
  168. }
  169. System.out.println("Got: " n);
  170. valueSet = false;
  171. notify();
  172. return n;
  173. }
  174. synchronized void put(int n) {
  175. while(valueSet)
  176. try {
  177. wait();
  178. } catch(InterruptedException e) {
  179. System.out.println("InterruptedException caught");
  180. }
  181. this.n = n;
  182. valueSet = true;
  183. System.out.println("Put: " n);
  184. notify();
  185. }
  186. }
  187. class Producer implements Runnable {
  188. Q q;
  189. oducer(Q q) {
  190. this.q = q;
  191. new Thread(this, "Producer").start();
  192. }
  193. public void run() {
  194. int i = 0;
  195. while(true) {
  196. q.put(i );
  197. }
  198. }
  199. }
  200. class Consumer implements Runnable {
  201. Q q;
  202. Consumer(Q q) {
  203. this.q = q;
  204. new Thread(this, "Consumer").start();
  205. }
  206. public void run() {
  207. while(true) {
  208. q.get();
  209. }
  210. }
  211. }
  212. class PCFixed {
  213. public static void main(String args[]) {
  214. Q q = new Q();
  215. new Producer(q);
  216. new Consumer(q);
  217. System.out.println("Press Control-C to stop.");
  218. }
  219. }
  220.  
  221. Inside get( ), wait( ) is called. This causes its execution to suspend until the Producer notifies you that some data is ready. When this happens, execution inside get( ) resumes. After the data has been obtained, get( ) calls notify( ). This tells Producer that it is okay to put more data in the queue. Inside put( ), wait( ) suspends execution until the Consumer has removed the item from the queue. When execution resumes, the next item of data is put in the queue, and notify( ) is called. This tells the Consumer that it should now remove it. Here is some output from this program, which shows the clean synchronous behavior:
  222. Put: 1
  223. Got: 1
  224. Put: 2
  225. Got: 2
  226. Put: 3
  227. Got: 3
  228. Put: 4
  229. Got: 4
  230. Put: 5
  231. Got: 5
  232.  
  233.  
  234.  
  235.  
  236. Q15.
  237.  
  238. class Complex
  239. {
  240. int Real,Imag;
  241. Complex()
  242. {}
  243. Complex(int Real1,int Imag1)
  244. {
  245. Real=Real1;
  246. Imag=Imag1;
  247. }
  248. Complex Add(Complex C1,Complex C2)
  249. {
  250. Complex CSum=new Complex();
  251. CSum.Real=C1.Real C2.Real;
  252. CSum.Imag=C1.Imag C2.Imag;
  253. return CSum;
  254. }
  255.  
  256. void Display()
  257. {
  258. System.out.println(Real " i" Imag);
  259. }
  260. }
  261.  
  262. class additionOfComplexNumbers
  263. {
  264. public static void main(String[] a)
  265. {
  266. Complex C1=new Complex(4,8);
  267. Complex C2=new Complex(5,7);
  268. Complex C3=new Complex();
  269. C3=C3.Add(C1,C2);
  270. System.out.print("SUM : ");
  271. C3.Display();
  272. }
  273. }
  274.  
  275.  
  276.  
  277. Q16.
  278.  
  279. public class StackDemo {
  280. private static final int capacity = 3;
  281. int arr[] = new int[capacity];
  282. int top = -1;
  283.  
  284. public void push(int pushedElement) {
  285. if (top < capacity - 1) {
  286. top ;
  287. arr[top] = pushedElement;
  288. System.out.println("Element " pushedElement
  289. " is pushed to Stack !");
  290. printElements();
  291. } else {
  292. System.out.println("Stack Overflow !");
  293. }
  294. }
  295.  
  296. public void pop() {
  297. if (top >= 0) {
  298. top--;
  299. System.out.println("Pop operation done !");
  300. } else {
  301. System.out.println("Stack Underflow !");
  302. }
  303. }
  304.  
  305. public void printElements() {
  306. if (top >= 0) {
  307. System.out.println("Elements in stack :");
  308. for (int i = 0; i <= top; i ) {
  309. System.out.println(arr[i]);
  310. }
  311. }
  312. }
  313.  
  314. public static void main(String[] args) {
  315. StackDemo stackDemo = new StackDemo();
  316.  
  317. stackDemo.pop();
  318. stackDemo.push(23);
  319. stackDemo.push(2);
  320. stackDemo.push(73);
  321. stackDemo.push(21);
  322. stackDemo.pop();
  323. stackDemo.pop();
  324. stackDemo.pop();
  325. stackDemo.pop();
  326. }
  327.  
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement