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

Similar documents
Midterm Examination (MTA)

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

Conditional Execution

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

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

CS141 Programming Assignment #6

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

AP Computer Science Unit 1. Programs

Formatted Output (printf) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

STUDENT LESSON A7 Simple I/O

CEN 414 Java Programming

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

ITERATION WEEK 4: EXMAPLES IN CLASS

Chapter 4: Control Structures I

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Question: Total Points: Score:

Lesson 7 Part 2 Flags

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Introduction to Computer Science Unit 2. Notes

Program Control Flow

Program Control Flow

Building Java Programs

Introduction to Java Applications

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

Programming with Java

AP Computer Science A Unit 2. Exercises

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

Object Oriented Programming. Java-Lecture 6 - Arrays

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Java Coding 3. Over & over again!

Chapter 5 Lab Methods

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Date: Dr. Essam Halim

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

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

Final Examination Semester 3 / Year 2012

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

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

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

Oct Decision Structures cont d

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

COMP 202 Java in one week

Lecture 5: Methods CS2301

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

Controls Structure for Repetition

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

Introduction to Computer Science Unit 2. Notes

Chapter 2. Elementary Programming

AP CS Unit 3: Control Structures Notes

CS111: PROGRAMMING LANGUAGE II

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Ahmadu Bello University Department of Mathematics First Semester Examinations June 2014 COSC211: Introduction to Object Oriented Programming I

CPSC 219 Extra review and solutions

Object Oriented Programming. Java-Lecture 1

First Programs. CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington

CSIS 10A Assignment 4 SOLUTIONS

CS141 Programming Assignment #8

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

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

St. Edmund Preparatory High School Brooklyn, NY

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Building Java Programs

Object-Oriented Programming in Java

International Journal of Advanced Research in Computer Science and Software Engineering

Full file at

Question: Total Points: Score:

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

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

Department of Computer Science Purdue University, West Lafayette

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Chapter 3. Selections

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Question: Total Points: Score:

AP CS A Exam Review Answer Section

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Elementary Programming

CS 139 Practice Midterm Questions #2

Loops. CSE 114, Computer Science 1 Stony Brook University

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

2.2 - Making Decisions

1. A company has vans to transport goods from factory to various shops. These vans fall into two categories: 50 items 150 items

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

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

Exceptions Handeling

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

Building Java Programs

Topic 11 Scanner object, conditional execution

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Transcription:

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014 Question One: Choose the correct answer and write it on the external answer booklet. 1. Java is. a. case sensitive b. object-oriented c. function-oriented d. a and b 2. Which of the following is not a valid identifier in Java? a. Char b. _book c. 6students d. $money 3. What is the exact output after executing the following statements? String word = "Hello"; System.out.println("It is " + word.charat(0)); a. It is H b. It is 'H' c. "It is H" d. It is "H" 4. What is the value of x after executing the following statement? double x = Math.floor(-1.8); a. -1.0 b. -2.0 c. 1.8 d. -1.8 5. What is the value of e after executing the following statement? double e = 9 % 2 / 2; a. 0.0 b. 0.5 c. 1.0 d. 2.0 6. What is the exact output after executing the following statements? int c = 1, d = 6; System.out.println("Sum = " + "c" + d); a. Sum = 7 b. Sum = cd c. Sum = c6 d. Sum = 16 Question Two: [11 marks] Write a Java class called Ellipse that reads from the user 2 integers that represent lengths of the semi-major axis and the semi-minor axis of an ellipse. Then calculates and prints the area of this ellipse (rounded to 1 decimal place) according to the following formula: M105 MTA Makeup Spring 2013/2014 Page 1 of 6

Question Three: [13 marks] a. For each of the following, write an equivalent Java statement assuming that all variables have been declared correctly and initialized: i. [2 marks] ii. using the method printf() instead of the method println() if you know that c is a String variable [2 marks] System.out.println("word = " + c); iii. using the ternary operator? : instead of the if statement if (x <= 25) y = x / 2; else y = 1; [2 marks] b. Trace the following pieces of code and give their exact output. [7 marks] i. [2 marks] int a = 7; double f = a + 2; System.out.printf("ans = %.2f\n", a + f); ii. [3 marks] String MTA = "Makeup"; if (MTA.equals("make-up")); System.out.println("YES"); if (MTA.equals("Makeup")) System.out.println("OK"); int x = 5; if (! (x > 9) && x > 3) System.out.println("NICE"); else System.out.println("NO"); iii. [2 marks] int m = 7; switch (m + 5) { case 1: System.out.println("ONE"); case 2: System.out.println("TWO"); default: System.out.println("???"); } System.out.printf("Now, m = %d\n", ++m); M105 MTA Makeup Spring 2013/2014 Page 2 of 6

Question Four: [8 marks] To get a driving license, you need to be at least 18 years old. Write a Java program that prompts the user to enter his/her age (as an integer) and displays a message on the screen if it is allowed for the user to get a driving license or not. Question Five: [22 marks] Write a Java program that prompts the user to enter 2 non-negative integers and print on the screen their average (rounded to 2 decimal places) and the largest of them. If the user enters 2 equal numbers, the message "The 2 numbers are equal" should be printed instead of the largest of them. If the user enters a negative value for one of the integers (or for both), nothing should be calculated and the message "Invalid Input" should be displayed. Sample Input: Enter the first number: 9 Enter the second number: 6 Sample Output: Average = 7.50 9 is larger Sample Input: Enter the first number: 9 Enter the second number: 9 Sample Output: Average = 9.00 The 2 numbers are equal Sample Input: Enter the first number: -3 Enter the second number: 9 Sample Output: Invalid Input M105 MTA Makeup Spring 2013/2014 Page 3 of 6

Solution Question One: Choose the correct answer and write it on the external answer booklet. Question No. Answer 1 D 2 C 3 A 4 B 5 A 6 C Question Two: [11 marks] Write a Java class called Ellipse that reads from the user 2 integers that represent lengths of the semi-major axis and the semi-minor axis of an ellipse. Then calculates and prints the area of this ellipse //Marks are distributed in the comments import java.util.scanner; //1 public class Ellipse { //1 for the header (including naming the class correctly) public static void main(string[] args) { Scanner input = new Scanner(System.in); //1 int a, b; //1 double area; //0.5 System.out.print("Enter length of the semi-major axis: "); //0.5 a = input.nextint(); //1 System.out.print("Enter length of the semi-minor axis: "); //0.5 b = input.nextint(); //1 area = Math.PI * a * b; //2 System.out.printf("The area of the ellipse = %.1f\n", area); //1.5 } // end main } // end class Question Three: [13 marks] a. For each of the following, write an equivalent Java statement assuming that all variables have been declared correctly and initialized: i. [2 marks] //1.5 for the expression //0.5 for using parentheses correctly z = Math.sqrt(x*x + y*y) / (2*x*y); M105 MTA Makeup Spring 2013/2014 Page 4 of 6

ii. [2 marks] //1 for %s //0.5 for \n //0.5 for, System.out.printf("word = %s\n", c); iii. [2 marks] // () are redundant and no marks should be deducted for not using them y = (x <= 25)? (x / 2) : 1; b. Trace the given pieces of code and give their exact output. iv. [2 marks] //1.5 for the correct output //0.5 for printing 2 digits after the decimal point ans = 16.00 v. [3 marks] //1 for each correct line of output //deduct 1 mark for each extra line YES OK NICE vi. [2 marks] //1 mark for each correct line of output //deduct 0.5 mark for each extra line??? Now, m = 8 M105 MTA Makeup Spring 2013/2014 Page 5 of 6

Question Four: [8 marks] //Marks are distributed in the comments import java.util.scanner; //1 public class Q4_MU { public static void main(string[] args) { Scanner input = new Scanner(System.in); //1 int age; //0.5 System.out.print("Enter your age: "); //0.5 age = input.nextint(); //1 if (age >= 18) //2 System.out.println("You can get a driving license"); //0.5 else //1 System.out.println("You cannot get a driving license"); //0.5 } // end main } // end class Question Five: [22 marks] //Marks are distributed in the comments import java.util.scanner; //1 public class Q5_MU { public static void main(string[] args) { Scanner input = new Scanner(System.in); //1 int num1, num2; //1 System.out.print("Enter the first number: "); //0.5 num1 = input.nextint(); //0.5 System.out.print("Enter the second number: "); //0.5 num2 = input.nextint(); //0.5 if (num1 >= 0 && num2 >= 0) //4 { //0.5 int sum; //0.5 double average; //0.5 sum = num1 + num2; //1 average = (double) sum / 2; //1.5 System.out.printf("Average = %.2f\n", average); //1.5 if (num1 == num2) //2 System.out.println("The 2 numbers are equal"); //0.5 else //1 System.out.println(Math.max(num1, num2) + " is larger"); //2.5 // Students could use?: or if-else to calculate the max } else //1 System.out.println("Invalid Input"); //0.5 } // end main } // end class M105 MTA Makeup Spring 2013/2014 Page 6 of 6