Midterm Examination (MTA)

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

CS141 Programming Assignment #6

Example: Monte Carlo Simulation 1

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods

CS141 Programming Assignment #8

AP Computer Science Unit 1. Programs

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

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

Conditional Execution

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

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

Chapter 5 Lab Methods

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

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

CS141 Programming Assignment #5

Introduction to Computer Science Unit 2. Notes

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

Introduction to Computer Science Unit 2. Notes

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

Programming with Java

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

1 Short Answer (10 Points Each)

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

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

Building Java Programs

2.2 - Making Decisions

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

Java Coding 3. Over & over again!

CEN 414 Java Programming

Introduction to Java Applications

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

Loops. CSE 114, Computer Science 1 Stony Brook University

Question: Total Points: Score:

Lesson 7 Part 2 Flags

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

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

AP Computer Science A Unit 2. Exercises

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

AP CS Unit 3: Control Structures Notes

! 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

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

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

2.8. Decision Making: Equality and Relational Operators

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

Object Oriented Programming. Java-Lecture 6 - Arrays

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

CS Week 5. Jim Williams, PhD

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

CSIS 10A Assignment 4 SOLUTIONS

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

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

Practice Midterm 1. Problem Points Score TOTAL 50

Building Java Programs

Oct Decision Structures cont d

Tutorial 03. Exercise 1: CSC111 Computer Programming I

PROGRAMMING FUNDAMENTALS

Program Control Flow

Program Control Flow

COMP 202 Java in one week

2. What are the two main components to the CPU and what do each of them do? 3. What is the difference between a compiler and an interpreter?

Supplementary Test 1

Controls Structure for Repetition

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

Question: Total Points: Score:

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

Chapter 5 Lab Methods

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Question: Total Points: Score:

Building Java Programs

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

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

ITERATION WEEK 4: EXMAPLES IN CLASS

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

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

Topic 11 Scanner object, conditional execution

AP COMPUTER SCIENCE A

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

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Question: Total Points: Score:

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

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

Building Java Programs

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

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

STUDENT LESSON A7 Simple I/O

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

Chapter 4: Control Structures I

Arrays. Eng. Mohammed Abdualal

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Fundamentals of Programming Data Types & Methods

CMPS 11 Introduction to Programming Midterm 1 Review Problems

Tutorial about Arrays

Topic 12 more if/else, cumulative algorithms, printf

Chapter 2. Elementary Programming

Topic 11 Scanner object, conditional execution

Repetition, Looping. While Loop

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

CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Transcription:

M105: Introduction to Programming with Java Midterm Examination (MTA) Spring 2013 / 2014 Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. 1. Compilers and Interpreters are used to convert programs into machine language? a. high-level language b. CPU c. assembly language d. a and c 2. Which of the following is a valid identifier in Java? a. HowAreYou? b. M105-MTA c. pricein$ d. static 3. What is the value of e after executing the following statement? double e = 9 / 2 * 2; a. 2.0 b. 2.25 c. 8.0 d. 9.0 4. What is the value of c after executing the following statement? double c = Math.ceil(2.4); a. 0.4 b. 2.0 c. 2.4 d. 3.0 5. What is the value of x after executing the following statement? boolean x =!(1 >= 1 && 0 <= 1); a. true b. false c. 0 d. 1 6. What is the value of y after executing the following statement? int y = (9 > 5)? 1 : 2; a. 1 b. 2 c. 12 d. 21 M105 MTA Spring 2013/2014 Page 1 of 6

Question Two: [13 marks] Write a Java class called Trapezoid that reads from the user 3 integers that represent the two base lengths of a trapezoid and its height. Then calculates and prints the area of this trapezoid (rounded to 1 decimal place) according to the following formula: ( ) Question Three: [12 marks] a. The following code includes 2 errors: [6 marks] int g = 6; if (g = 6) System.out.printf("g = %.2f\n", g); i. Find these errors and classify them as a compilation error, a fatal logical error or a non-fatal logical error. [4 marks] ii. Rewrite the above code after correcting the errors. [2 marks] b. Trace the following pieces of code and give their exact output. [6 marks] i. [2 marks] int d = 3; double f = 5.684; System.out.printf("d = %d\nf = %.2f\n", d, f); ii. [2 marks] String course = "M105"; System.out.println(course.charAt(0)); if (course.equals("m105")) System.out.println("YES"); else System.out.println("NO"); iii. [2 marks] int z = 7; switch (z % 3) { case 0: System.out.println("ZERO"); case 1: System.out.println("ONE"); break; default: System.out.println("???"); } System.out.printf("Now, z = %d\n", z++); M105 MTA Spring 2013/2014 Page 2 of 6

Question Four: [12 marks] Write a Java program that prompts the user to enter 2 words and displays on the screen a message if the 2 words start with the same character or not. Question Five: [17 marks] Write a Java program that prompts the user to enter 2 non-negative integers and print on the screen their sum and a message explains if this sum is odd or even. 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: 7 Sample Output: Sum = 16 which is even Sample Input: Enter the first number: 9 Enter the second number: 8 Sample Output: Sum = 17 which is odd Sample Input: Enter the first number: -3 Enter the second number: 9 Sample Output: Invalid Input M105 MTA Spring 2013/2014 Page 3 of 6

Solution Question One: [6 marks] Choose the correct answer and write it on the external answer booklet. Question No. Answer 1 A 2 C 3 C 4 D 5 B 6 A Question Two: [13 marks] Write a Java class called Trapezoid that reads from the user 3 integers that represent the two base lengths of a trapezoid and its height. Then calculates and prints the area of this trapezoid //Marks are distributed in the comments import java.util.scanner; //1 public class Trapezoid { //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, h; //1.5 double area; //0.5 System.out.print("Enter the first base length of the trapezoid: "); //0.5 a = input.nextint(); //1 System.out.print("Enter the second base length of the trapezoid: "); //0.5 b = input.nextint(); //1 System.out.print("Enter the height of the trapezoid: "); //0.5 h = input.nextint(); //1 area = (double)(a+b) / 2 * h; //2 System.out.printf("The area of the trapezoid = %.1f\n", area); //1.5 } // end main } // end class M105 MTA Spring 2013/2014 Page 4 of 6

Question Three: [12 marks] a. The given code includes 2 errors [6 marks] 1 i. Find these errors and classify them as a compilation error, a fatal logical error or a non-fatal logical error. [4 marks] if (g = 6) The code The error The classification 2 System.out.printf( "g = %.2f\n", g); The condition of if should be a boolean expression. //1 mark g is an integer variable and the format specifier %.2f is not suitable for it. //1 mark compilation error //1 mark fatal logical error //1 mark ii. Rewrite the given code after correcting the errors. //1 mark for each correction int g = 6; if (g == 6) System.out.printf("g = %d\n", g); [2 marks] b. Trace the given pieces of code and give their exact output. [6 marks] i. [2 marks] //1 mark for each correct line of output d = 3 f = 5.68 ii. [2 marks] //1 mark for each correct line of output //deduct 1 mark for each extra line M NO iii. [2 marks] //1 mark for each correct line of output //deduct 0.5 mark for each extra line ONE Now, z = 7 M105 MTA Spring 2013/2014 Page 5 of 6

Question Four: [12 marks] //Marks are distributed in the comments import java.util.scanner; //1 public class Q4 { public static void main(string[] args) { Scanner input = new Scanner(System.in); //1 String word1, word2; //1 System.out.print("Enter the first word: "); //0.5 word1 = input.next(); //1 System.out.print("Enter the second word: "); //0.5 word2 = input.next(); //1 if (word1.charat(0) == word2.charat(0)) //4 System.out.println("The 2 words start with the same character"); //0.5 else //1 System.out.println("The 2 words do not start with the same character"); //0.5 } // end main } // end class Question Five: [17 marks] //Marks are distributed in the comments import java.util.scanner; //1 public class Q5 { 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) //3.5 { //0.5 int sum; //0.5 sum = num1 + num2; //1 System.out.print("Sum = " + sum); //1 if (sum % 2 == 0) //2 System.out.println(" which is even"); //0.5 else //1 System.out.println(" which is odd"); //0.5 } else //1 System.out.println("Invalid Input"); //0.5 } // end main } // end class M105 MTA Spring 2013/2014 Page 6 of 6