Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Similar documents
Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CIS 1068 Design and Abstraction Spring 2017 Midterm 1a

Midterm Examination (MTA)

Question: Total Points: Score:

AP CS Unit 3: Control Structures Notes

CS141 Programming Assignment #6

Introduction to Computer Science Unit 2. Notes

AP COMPUTER SCIENCE A

Introduction to Computer Science Unit 2. Notes

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

1 Short Answer (10 Points Each)

CSCI 1226 Sample Midterm Exam

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Oct Decision Structures cont d

Final Examination Semester 2 / Year 2011

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

CONCORDIA UNIVERSITY Summer 2005 Comp 248 /1 Section AA Introduction to Programming Final Examination/A

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Controls Structure for Repetition

Computer Programming, I. Laboratory Manual. Final Exam Solution

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

CSCI 135 Exam #0 Fundamentals of Computer Science I Fall 2013

Question: Total Points: Score:

CSE 142, Autumn 2010 Midterm Exam, Friday, November 5, 2010

6 COMPUTER PROGRAMMING

Question: Total Points: Score:

CSE 142, Summer 2013 Midterm Exam, Friday, July 26, 2013

CSE 142, Autumn 2011 Midterm Exam: Friday, November 4, 2011

New York University Introduction to Computer Science Midterm2B Sample Problems Solution Key. Instructions:

University of Palestine. Mid Exam Total Grade: 100

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

COE 212 Engineering Programming. Welcome to Exam II Tuesday November 28, 2018

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Introduction to Computer Science Unit 2. Exercises

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

Ryerson University Vers HAL6891A-05 School of Computer Science CPS109 Midterm Test Fall 05 page 1 of 6

Arrays. Eng. Mohammed Abdualal

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

CSC 1051 Data Structures and Algorithms I

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

Assignment 2.4: Loops

Darrell Bethea May 25, 2011

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

1. What is the difference between a compiler and an interpreter? Also, discuss Java s method.

1 Short Answer (15 Points Each)

Programming with Java

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

King Saud University College of Computer and Information Sciences Computer Science Department

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

CS111: PROGRAMMING LANGUAGE II

SAMPLE QUESTIONS FOR DIPLOMA IN INFORMATION TECHNOLOGY; YEAR 1

CSC141, Computer Science I, Instructor: Dr. Zhen Jiang, Test 2

University of Massachusetts Amherst, Electrical and Computer Engineering

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

CSCE 145 Exam 1 Review. This exam totals to 100 points. Follow the instructions. Good luck!

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

CS 101 Exam 2 Spring Id Name

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CPSC 219 Extra review and solutions

Building Java Programs

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Section 2.2 Your First Program in Java: Printing a Line of Text

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Selection Statements and operators

CS 170 Exam 2. Version: A Spring Name (as in OPUS) (print): Instructions:

LAB 13: ARRAYS (ONE DIMINSION)

CS 177 Spring 2010 Exam I

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Practice Midterm 1. Problem Points Score TOTAL 50

New York University Introduction to Computer Science Midterm2 Sample Problems 2013 Andrew I. Case. Instructions:

Control Statements: Part 1

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CIS 110 Introduction to Computer Programming Spring 2016 Midterm

COMP-202: Foundations of Programming. Lecture 2: Variables, and Data Types Sandeep Manjanna, Summer 2015

private static final char[] Alphabet = "abcdefghijklmnopqrstuvwxyz".tochararray();

CIS October 19, 2017

M e t h o d s a n d P a r a m e t e r s

Question: Total Points: Score:

AP Computer Science A Unit 2. Exercises

Midterm Practice Problems

CSC 1051 Algorithms and Data Structures I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Faculty of Science Midterm. COMP-202B - Introduction to Computing I (Winter 2008)

Handout 4 Conditionals. Boolean Expressions.

Transcription:

كلية الحاسبات وتقنية المعلوما 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