Part I. Multiple Choice Questions (2 points each):



Similar documents
J a v a Quiz (Unit 3, Test 0 Practice)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Java

Sample CSE8A midterm Multiple Choice (circle one)

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Java Interview Questions and Answers

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

AP Computer Science Java Subset

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Chapter 2 Introduction to Java programming

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

(Eng. Hayam Reda Seireg) Sheet Java

CS 111 Classes I 1. Software Organization View to this point:

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Topic 11 Scanner object, conditional execution

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

More on Objects and Classes

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Arrays in Java. Working with Arrays

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Chapter 2 Elementary Programming

Programmierpraktikum

Java Crash Course Part I

java Features Version April 19, 2013 by Thorsten Kracht

CS170 Lab 11 Abstract Data Types & Objects

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Java CPD (I) Frans Coenen Department of Computer Science

Install Java Development Kit (JDK) 1.8

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

History OOP languages Year Language 1967 Simula Smalltalk

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Building Java Programs

LAB4 Making Classes and Objects

Programming Languages CIS 443

AP Computer Science Java Mr. Clausen Program 9A, 9B

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

CS114: Introduction to Java

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Java Programming Fundamentals

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Introduction to Programming

Basic Programming and PC Skills: Basic Programming and PC Skills:

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Example of a Java program

Building Java Programs

Using Files as Input/Output in Java 5.0 Applications

13 File Output and Input

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

CS 121 Intro to Programming:Java - Lecture 11 Announcements

LOOPS CHAPTER CHAPTER GOALS

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Explain the relationship between a class and an object. Which is general and which is specific?

JAVA ARRAY EXAMPLE PDF

Comp 248 Introduction to Programming

Iteration CHAPTER 6. Topic Summary

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

AP Computer Science Static Methods, Strings, User Input

The Interface Concept

Building Java Programs

Programming in Java. What is in This Chapter? Chapter 1

Chapter 1 Fundamentals of Java Programming

CS 106 Introduction to Computer Science I

Object-Oriented Programming in Java

C++FA 5.1 PRACTICE MID-TERM EXAM

Implementation Aspects of OO-Languages

Chapter 2: Elements of Java

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Programming Fundamentals I CS 110, Central Washington University. November 2015

Java Basics: Data Types, Variables, and Loops

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

15-214: Principles of Software Construction 8 th March 2012

Some Scanner Class Methods

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

Introduction to Java Lecture Notes. Ryan Dougherty

Building Java Programs

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Moving from CS 61A Scheme to CS 61B Java

Lab 5: Bank Account. Defining objects & classes

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Transcription:

Part I. Multiple Choice Questions (2 points each): 1. Which of the following is NOT a key component of object oriented programming? (a) Inheritance (b) Encapsulation (c) Polymorphism (d) Parallelism ****** 2. Which of these is TRUE of the relationship between objects and classes? (a) A class is an instance of an object. (b) An object is the ancestor of its subclass. (c) An object is an instance of a class. ****** (d) An object is the descendant of its superclass. 3. The Java compiler translates source code into (a) machine code. (b) Assembly code. (c) Byte code. ****** (d) JVM code. 4. Which of the following is NOT a valid type in Java? (a) void (b) int (c) Integer (d) static ****** 5. Which of the following statements is NOT correct? (a) We can use a new operator on String to create a "String" object. (b) We can use the new operator on int to create an "int" object. ****** (c) Variables of type "int" can be assigned a value just after being declared. (d) Variables of type "String" can be assigned a value just after being declared. 6. Which of the following statements can provide the output "180"? (a) String str = "CS180"; System.out.print(str.substring(2, 4)); (b) String str = "CS180"; System.out.print(str.substring(3, 5)); (c) String str = "P"; System.out.print(str.length() + "80"); ****** (d) String str = "P"; System.out.print(str.length() + 80); 1

7. What is the output of the following code if the input string is "CS 180"? Scanner scanner = new Scanner(); String str; str = scanner.next(); System.out.print(str); (a) CS180 (b) CS (c) CS 180 (d) The above code fragment does not compile. ****** 8. Which of the following is NOT true? (a) Both float and double represent real numbers. (b) long can present a larger range of values than double. ****** (c) The size of long and double is the same. (d) char is not a numeric data type. 9. Which of the following is TRUE about the piece of JAVA code below if we judge the statements independently? final double PI; int radius = Integer.parseInt("3.5"); double area = Math.PI * Math.pow(radius, 2); (a) The third statement computes the area of a circle whose radius is in variable radius. ****** (b) The first statement correctly declares the constant PI. (c) The second statement correctly obtains the radius from the given string. (d) The third statement incorrectly uses Math.pow. 10. What is the value of y when the code below is executed? int x = 4; int y = (int)math.ceil(x % 5 + x / 5.0); (a) 1 (b) 6 (c) 5 ****** (d) 4 11. Which of the following statements is TRUE? (a) The Java compiler always adds a default constructor to a user defined class. (b) Each instantiated object will have its own copy of a class variable. (c) When an object is passed to a method, a copy of each of the object s data members are created and passed to the method. (d) A class can have multiple constructors. ****** 2

12. What is the range of the random number r generated by the code below? int r = (int)(math.floor(math.random() * 8)) + 2; (a) 3 <= r <= 10 (b) 3 <= r <= 9 (c) 2 <= r <= 10 (d) 2 <= r <= 9 ****** 13. If a local variable of a method shop() belonging to a class called Walmart has the same name as a data member of Walmart, which value is used when shop() is executing? (a) the local variable s ****** (b) the class variable s (c) the data member s (d) None of the above since this would cause a compiler error. 14. What will be printed by the code fragment below? double height = 5.5; if(height-- >= 5.0) System.out.print("tall "); if(--height >= 4.0) System.out.print("average "); if(height-- >= 3.0) System.out.print("short "); System.out.print("very short "); (a) short (b) average short (c) tall short ****** (d) tall 15. Translate this statement into Java: If the value of temperature is in between 20.0 and 40.0, print very cold. (a) if(!(temperature < 20.0 temperature > 40.0)) ****** System.out.println("very cold"); (b) if(20.0 <= temperature <= 40.0) System.out.println("very cold"); (c) if(temperature >= 20.0 temperature <= 40.0) System.out.println("very cold"); (d) if(temperature >= 20.0 temperature <= 40.0) System.out.println("very cold"); 3

16. What is the value of n after executing the following code? int n = 20; switch(n) case 10: n = n + 1; case 15: n = n + 2; case 20: n = n + 3; case 25: n = n + 4; case 30: n = n + 5; (a) This code does not compile. (b) 25 (c) 32 ****** (d) 23 17. What is the value of n after executing the following code? int n = 20; int p = n + 5; int q = p - 10; int r = 2 * (p - q); switch(n) case p: n = n + 1; case q: n = n + 2; case r: n = n + 3; default: n = n + 4; (a) 24 (b) This code does not compile. ****** (c) 20 (d) 27 4

18. What is the value of variable z after executing the following code? int x = 5; int y = 5; int z = 5; if (x > 3) if (y > 4) if (z > 5) z += 1; z += 2; z += 3; z += 4; (a) 9 (b) 5 (c) 11 ****** (d) 7 19. What is the output of the following program? public class Test public static void main( String[] args ) private static final int value = 5; float total; total = value + value / 2; System.out.println( total ); (a) 7.5 (b) 7.0 (c) 5.0 (d) None of the above ****** 20. Which of the following variables contains null? String a; String b = new String(); String c = ""; String d = "null"; (a) a ****** (b) b (c) c (d) d (this answer also accepted) 5

This page left blank intentionally. 6

Part II. Programming Questions (60 points total): 1. (20 points) Write a program that prompt for and reads a power of 10 (e.g. 6, 9, 10,...). It should then displays how big the number is in English (e.g. in Million, Billion, etc.). Display an appropriate message for the input value that has no corresponding word. Here are some output examples: 10 raised to the 6th power is a million. 10 raised to the 12th power is a trillion. There is no single word for 10 raised to the 10th power. The table below shows the correspondence between the power of 10 and the word representing the number. Any input value that is outside of the table should be handled as one that has no corresponding word. Power of 10 Number 6 Million 9 Billion 12 Trillion 15 Quadrillion 18 Quintillion 21 Sextillion 30 Nonillion 100 Googol You do not need to worry about special ordinal numbers such as 1st, 2nd, 3rd, and so on. That is, you can use "th" for all ordinal numbers. For example: 10 raised to the 21th power is a sextillion. There is no single word for 10 raised to the 2th power. Note: You MUST USE the switch statement. Write your code on the next page. 7

Solution for programming question 1: import java.util.scanner; public class readpower public static void main(string[] args) Scanner s = new Scanner(System.in); System.out.print("Input a positive integer: "); int power = s.nextint(); boolean valid = true; String word = ""; switch (power) case 6: word = "million"; case 9: word = "billion"; case 12: word = "trillion"; case 15: word = "quadrillion"; case 18: word = "quintillion"; case 21: word = "sextillion"; case 30: word = "nonillion"; case 100: word = "googol"; default: valid = false; if (valid) System.out.println("10 raised to the " + power + "th power is a " + word + "."); System.out.println("There is no single word for 10 raised to the " + power + "th power."); 8

2. (20 points) Define a class called Triangle with three integer data members a, b, and c as the lengths of its three edges. This class should have the following methods: (a) a constructor with 3 parameters representing the 3 edges (b) a method istriangle() which returns true if the 3 edges are all positive and they satisfy the triangle inequality where a+b > c, a+c > b, b+c > a. (c) a method getangle() with 1 parameter, an edge, which returns the angle in degrees of the angle facing the given edge. The signature of these methods are given below: public Triangle(int newa, int newb, int newc) public boolean istriangle() public double getangle(int edge) Note: getangle() should return zero if the triangle is not really a triangle. Also, here are a few formulas to help you define the class: FYI, if A is the angle facing side a, then the following formula should help: cosa = b2 + c 2 a 2 2bc Note: Write your code on the next page. It must be a complete class. 9

Solution for programming question 2: public class Triangle private int s1; private int s2; private int s3; public Triangle(int a, int b, int c) s1 = a; s2 = b; s3 = c; public boolean istriangle() return (s1 > 0 && s2 > 0 && s3 > 0 && ((s1+s2)>s3) && ((s2+s3)>s1) && ((s1+s3)>s2)); // compute angle facing the given side public double getangle(int side) double angle; if (!istriangle()) return 0; if (side == s1) angle = Math.acos((s2*s2 + s3*s3 - s1*s1)/(2.0 * s2 * s3)); if (side == s2) angle = Math.acos((s1*s1 + s3*s3 - s2*s2)/(2.0 * s1 * s3)); if (side == s3) angle = Math.acos((s2*s2 + s1*s1 - s3*s3)/(2.0 * s2 * s1)); angle = 0; angle = angle * 180.0 / Math.PI; return angle; 10

3. (20 points) For this question you are provided with the class Student that has at least three methods available to you. getscore() returns the student s score. isattendancegood() returns true if the student has good attendance, false otherwise. istalkative() returns true if the student is talkative in class, false otherwise. The signature of these methods are given below: public int getscore() public boolean isattendancegood() public boolean istalkative() Note that the class may have more data members or methods that are not reflected here. You are asked to write a method printgrade with the following signature: void printgrade(student s1); As the name suggests, this method takes an object of type Student and prints out the final grade for that student. The grade is assigned following these guidelines. (a) There are only 3 possible grades A, B or C. (b) If a student has a score of more than 80, they have an A. (c) If a student has a score of more than 60 but less than 80, they have a B. (d) If a student has a score of less than 60 they have a C. (e) Students that fall on the boundary of an A and a B (i.e., a score of exactly 80) receive an A only if they have good attendance, and a B otherwise. (f) Students that fall on the boundary of a B and a C (i.e., a score of exactly 60) receive a C only if they are talkative in class, and a B otherwise. Note: Write your code on the next page. 11

Solution for programming question 3: void printgrade(student s1) int score = s1.getscore(); if (score >= 80) if (score == 80) if (s1.goodattendance()) System.out.println("A"); System.out.println("B"); System.out.println("A"); if (score < 80 && score >= 60) if (score == 60) if (s1.talkative()) System.out.println("C"); System.out.println("B"); System.out.println("B"); if (score < 60) System.out.println("C"); 12