كلية الحاسبات وتقنية المعلوما Exam 2 Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: November 22, 2015 Student Name: Student ID: Total Marks: 40 Obtained Marks: Instructions: Do not open this exam booklet until you are directed to do so. This exam will end in Two Hours (120 Mins) Write your full name and Student registration No. clearly on the first page. When the exam is started, write your complete Student Registration No. clearly on the top of *EVERY* page. Write your solution in the space provided. If you need more space, write on the back of the sheet containing the problem. If still you need more space then you can use extra sheets. In the case of extra sheet, clearly mention the question number whose answer you are giving and you student registration number on the extra sheet. Plan your time wisely. Do not spend too much time on any one problem. Read through all of them first and attack them in order that allows you to make the most progress. Show your work, as partial credit will be given. You will be graded not only on the correctness of your answer, but also on the clarity and method correctness with which you express your answer. Good luck. 1
Question 1: Fill in the blanks (40) a) Consider the following program and give the output of the program. (2) public class Question public static void main String args int n 0; whilen 5 n; if n 1 n 3 continue; System.out.printlnn; 2 4 5 6 b) Identify the errors in the following program and correct them. (5) 1 public class Test 2 public static void mainstring args 3 int sum 0; 4 for int i 0; i 10; i; 5 sum i; 6 int i, j; 7 if i j; 8 System.out.printlni 9 else 10 System.out.printlnj; 11 12 while j 10; 13 14 j; 15 ; 16 17 do 18 j; 19 while j 10 20 21 4 for int i 0; i 10; i 7 if i j 8 System.out.printlni; 12 while j 10 19 while j 10; c) Evaluate the following Boolean expressions: (3) (1) 2 * 2 3 > 2 && 4 2 > 5 False (2) 2 * 2 3 > 2 4 2 > 5 False (3) 2 * 2 3 > 2 ^ 8 2 > 5 True 2
d) Rewrite the following IF statement using the conditional expression: (2) if temperature 90 pay pay * 1.5; else pay pay * 1.1; pay = (temperature > 90)? pay * 1.5 : pay * 1.1; e) Using switch statement, rewrite the following IF statement. (5) if a 1 x 5; else if a 2 x 10; else if a 3 x 16; else if a 4 x 34; Switcha case 1: x5; case 2: x10; case 3: x16; case 4: x34; f) Write infinite loops using the for, do-while and while loops that prints the message Infinite Loop (6) FOR LOOP for ( ; ; ) System.out.println( Infinite Loop ); DO-WHILE LOOP do System.out.println( Infinite Loop ); while(true); WHILE LOOP while(true) System.out.println( Infinite Loop ); 3
g) Consider the following program and give the output of the program. (2) public class Question public static void main String args int n 0; whilen 5 n; if n 3 System.out.printlnn * 2; 2 4 4
h) Write a program that displays a random English Lowercase Letter using the Math.random() method. (5) Solution 1 public class RandomEnglishCharacters{ public static void main (String [] args){ int randomnumber = (int) (Math.random() * 100); char randomcharacter = (char) ((randomnumber % 26) + 97); System.out.print( Random English Character is + randomcharacter); Solution 2 public class RandomEnglishCharacters{ public static void main (String [] args){ System.out.print( Random English Character is + (char) (Math.random()*27 + a ) ); 5
i) Write a program using for loop that displays all the numbers from 100 to 1000, ten numbers per line that are divisible by 5 and 6. (5) Sample output of the program is: 120 150 180 210 240 270 300 330 360 390 420 450 480 510 540 570 600 630 660 690 720 750 780 810 840 870 900 930 960 990 public class Numbers{ public static void main(string [] args){ int n = 0; for (int i = 100; i <=1000; i++){ if(i % 5 == 0 && i % 6 == 0){ System.out.print(i + \t ); n++; if (n == 10){ n = 0; System.out.print( \n ); 6
j) Write a Java program that prints the following pattern on the screen using do-while loop. (5) 10 9 8 7 6 5 4 3 2 1 10 9 8 7 6 5 4 3 2 10 9 8 7 6 5 4 3 10 9 8 7 6 5 4 10 9 8 7 6 5 10 9 8 7 6 10 9 8 7 10 9 8 10 9 10 public class NumberPyramid{ public static void main (String [] args){ int i = 1, j; do{ j = 10; do{ System.out.print(j + ); j--; while(j>=i); System.out.print( \n ); i++; while (i <= 10); 7
BONUS Question (10) A short value is stored in 16 bits after converting the integer into binary number. Write a program that prompts the user to enter a short integer and displays the 16 bits for the integer. Here are sample runs: Enter an integer: 5 The bits are 0000 0000 0000 0101 import java.util.scanner; public class BonusQuestion{ public static void main (String [] args){ Scanner input = new Scanner(System.in); int digits=0, rem, num; short n; String res = ; System.out.print( Enter and integer: ); n = input.nextshort(); num = n; while (num!= 0){ rem = num % 2; num = num / 2; res = rem + res; digits++; if (digits < 16){ for (int i = digits; i < 16; i++) res = 0 + res; System.out.println( 16 bits for short number + n + are + res); 8