Exam JP2011(Java Programming) MidTerm Test 5/ Name RegNo % 1 is. a) 1 b) 2 c) 3 d) 4 e) 0

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

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

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

Java Interview Questions and Answers

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

AP Computer Science Java Subset

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

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

The C Programming Language course syllabus associate level

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

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

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

Introduction to Java

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

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

Java Application Developer Certificate Program Competencies

C++FA 5.1 PRACTICE MID-TERM EXAM

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

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Computer Programming I

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

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

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

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

Fundamentals of Java Programming

Java CPD (I) Frans Coenen Department of Computer Science

Habanero Extreme Scale Software Research Project

Java (12 Weeks) Introduction to Java Programming Language

Inheritance, overloading and overriding

java Features Version April 19, 2013 by Thorsten Kracht

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

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

Java Programming Fundamentals

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

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

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

C++ INTERVIEW QUESTIONS

CS193j, Stanford Handout #10 OOP 3

El Dorado Union High School District Educational Services

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

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

Glossary of Object Oriented Terms

Introduction to Java. CS 3: Computer Programming in Java

Moving from CS 61A Scheme to CS 61B Java

Java Basics: Data Types, Variables, and Loops

Sample CSE8A midterm Multiple Choice (circle one)

Crash Course in Java

Description of Class Mutation Mutation Operators for Java

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

Chapter 1 Fundamentals of Java Programming

Chapter 1 Java Program Design and Development

Java SE 8 Programming

Pemrograman Dasar. Basic Elements Of Java

Part 3: GridWorld Classes and Interfaces

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

CS 106 Introduction to Computer Science I

Object Oriented Software Design

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

Example of a Java program

Object Oriented Software Design

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

An Incomplete C++ Primer. University of Wyoming MA 5310

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

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

Computing Concepts with Java Essentials

Object-Oriented Programming in Java

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Chapter 13 - Inheritance

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Arrays. Introduction. Chapter 7

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Java Programming Language

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:

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

Computer Science III Advanced Placement G/T [AP Computer Science A] Syllabus

Contents. 9-1 Copyright (c) N. Afshartous

#820 Computer Programming 1A

More on Objects and Classes

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

Sources: On the Web: Slides will be available on:

Programming Languages CIS 443

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

VB.NET Programming Fundamentals

Chapter 2 Introduction to Java programming

Statements and Control Flow

Object-Oriented Programming: Polymorphism

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

Short Introduction to the Concepts of Programming in Java Overview over the most important constructs

RARITAN VALLEY COMMUNITY COLLEGE ACADEMIC COURSE OUTLINE. CISY 105 Foundations of Computer Science

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

Building Java Programs

public static void main(string[] args) { System.out.println("hello, world"); } }

Under the Hood: The Java Virtual Machine. Lecture 24 CS 2110 Fall 2011

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

In this Chapter you ll learn:

Introduction to Programming System Design. CSCI 455x (4 Units)

Transcription:

Exam JP2011(Java Programming) MidTerm Test 5/6 2011 Name RegNo 1. 25 % 1 is. a) 1 b) 2 c) 3 d) 4 e) 0 2. -24 % -5 is. a) 3 b) -3 c) 4 d) -4 e) 0 3. Which of these data types requires the most amount of memory? a) long b) int c) byte d) short 4. What is the printout of the following code: double x = 5.5; int y = (int)x; System.out.println(ʺx is ʺ + x + ʺ and y is ʺ + y); a) x is 5.5 and y is 5.0 b) x is 6 and y is 6 c) x is 5 and y is 6 d) x is 5.5 and y is 5 e) x is 6.0 and y is 6.0 5. Suppose x is 1. What is x after x -= 1? a) 0 b) 1 c) 2 d) -1 e) -2 6. The expression ʺJava ʺ + 1 + 2 + 3 evaluates to. a) java 123 b) Java 123 c) Java6 d) Java123 e) Illegal expression 7. What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0? a) true b) false c) There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true. 8. What is the printout of the following switch statement? char ch = ʹbʹ; switch (ch) { case ʹaʹ: System.out.print(ch); case ʹbʹ: System.out.print(ch); case ʹcʹ: System.out.print(ch); case ʹdʹ: System.out.print(ch); a) abcd b) bb c) bcd d) b e) bbb 9. The following code displays. double temperature = 50; if (temperature >= 100) System.out.println(ʺtoo hotʺ); else if (temperature <= 40) System.out.println(ʺtoo coldʺ); else System.out.println(ʺjust rightʺ); a) too cold b) just right c) too hot d) too hot too cold just right 10. What is the number of iterations in the following loop: for (int i = 1; i < n; i++) { // iteration a) n + 1 b) n - 1 c) 2*n d) n 11. (int)(ʹaʹ + Math.random() * (ʹzʹ - ʹaʹ + 1)) returns a random number. a) between (int)ʹaʹ and (int)ʹzʹ b) between ʹaʹ and ʹzʹ c) between ʹaʹ and ʹyʹ d) between 0 and (int)ʹzʹ 1

12. Analyze the following code fragments that assign a boolean value to the variable even. Code 1: if (number % 2 == 0) even = true; else even = false; Code 2: even = (number % 2 == 0)? true: false; Code 3: even = number % 2 == 0; a) Code 2 has a compile error, because you cannot have true and false literals in the conditional expression. b) All three are correct, but Code 2 is preferred. c) All three are correct, but Code 1 is preferred. d) All three are correct, but Code 3 is preferred. e) Code 3 has a compile error, because you attempt to assign number to even. 13. How many times will the following code print ʺWelcome to Javaʺ? int count = 0; do { System.out.println(ʺWelcome to Javaʺ); while (++count < 10); a) 8 b) 10 c) 9 d) 11 e) 0 14. How many times will the following code print ʺWelcome to Javaʺ? int count = 0; do { System.out.println(ʺWelcome to Javaʺ); count++; while (count < 10); 15. Analyze the following fragment: double sum = 0; double d = 0; while (d!= 10.0) { d += 0.1; sum += sum + d; a) The program never stops because d is always 0.1 inside the loop. b) The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers. c) After the loop, sum is 0 + 0.1 + 0.2 + 0.3 +... + 1.9 d) The program does not compile because sum and d are declared double, but assigned with integer value 0. 16. Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; a) Yes b) No 17. Which of the following statements are correct to invoke the printmax method in Listing 6.5 in the textbook? (Choose all that apply.) a) printmax(new double[ ]{1, 2, 3); b) printmax(1.0, 2.0, 2.0, 1.0, 4.0); c) printmax(1, 2, 2, 1, 4); d) printmax(new int[ ]{1, 2, 3); 18. What is Math.ceil(3.6)? a) 4.0 b) 3 c) 5.0 d) 3.0 a) 9 b) 10 c) 0 d) 11 e) 8 2

19. The selectionsort method is defined in this section. What is list1 after executing the following statements? double[ ] list1 = {3.1, 3.1, 2.5, 6.4; selectionsort(list1); a) list1 is 3.1, 2.5, 3.1, 6.4 b) list1 is 6.4, 3.1, 3.1, 2.5 c) list1 is 3.1, 3.1, 2.5, 6.4 d) list1 is 2.5 3.1, 3.1, 6.4 20. Analyze the following code: class Test { System.out.println(xmethod(5)); public static int xmethod(int n, long t) { System.out.println(ʺintʺ); return n; public static long xmethod(long n) { System.out.println(ʺlongʺ); return n; a) The program displays long followed by 5. b) The program displays int followed by 5. c) The program runs fine but displays things other than 5. d) The program does not compile because the compiler cannot distinguish which xmethod to invoke. 22. What is k after the following block executes? { int k = 2; nprint(ʺa messageʺ, k); System.out.println(k); a) 1 b) 2 c) 0 d) k is not defined outside the block. So, the program has a compile error 23. In the following code, what is the printout for list2? class Test { int[ ] list1 = {1, 2, 3; int[ ] list2 = {1, 2, 3; list2 = list1; list1[0] = 0; list1[1] = 1; list2[2] = 2; for (int i = 0; i < list2.length; i++) System.out.print(list2[i] + ʺ ʺ); a) 0 1 2 b) 1 1 1 c) 0 1 3 d) 1 2 3 24. Which of the following statements is valid? (Choose all that apply.) a) double d[ ] = new double[30]; b) int[ ] i = {3, 4, 3, 2; c) int i = new int(30); d) char[ ] c = new char(); e) char[ ] c = new char[4]{ʹaʹ, ʹbʹ, ʹcʹ, ʹdʹ; 21. The keyword is required to declare a class. a) class b) public c) private d) All of the above. 3

25. Show the output of the following code: int[ ] x = {1, 2, 3, 4, 5; increase(x); int[ ] y = {1, 2, 3, 4, 5; increase(y[0]); System.out.println(x[0] + ʺ ʺ + y[0]); public static void increase(int[ ] x) { for (int i = 0; i < x.length; i++) x[i]++; public static void increase(int y) { y++; a) 2 1 b) 2 2 c) 1 2 d) 1 1 e) 0 0 26. The default value for data field of a boolean type, numeric type, object type is, respectively. a) false, 1, null b) true, 0, null c) true, 1, Null d) true, 1, null e) false, 0, null 27. Suppose you wish to provide an accessor method for a boolean property finished, what signature of the method should be? a) public void getfinished() b) public boolean isfinished() c) public void isfinished() d) public boolean getfinished() 29. Analyze the following code: boolean[ ][ ] x = new boolean[3][ ]; x[0] = new boolean[1]; x[1] = new boolean[2]; x[2] = new boolean[3]; System.out.println(ʺx[2][2] is ʺ + x[2][2]); a) The program has a compile error because new boolean[3][ ] is wrong. b) The program runs and displays x[2][2] is true. c) The program runs and displays x[2][2] is false. d) The program has a runtime error because x[2][2] is null. e) The program runs and displays x[2][2] is null. 30. When invoking a method with an object argument, is passed. a) the reference of the object b) a copy of the object c) the object is copied, then the reference of the copied object d) the contents of the object 31. Suppose a method p has the following heading: public static int[ ][ ] p() What return statement may be used in p()? a) return int[ ]{1, 2, 3; b) return new int[ ][ ]{{1, 2, 3, {2, 4, 5; c) return new int[ ]{1, 2, 3; d) return {1, 2, 3; e) return 1; 28. An object is an instance of a. a) class b) method c) program d) data 4

32. What is the value of mycount.count displayed? Count mycount = new Count(); int times = 0; for (int i=0; i<100; i++) increment(mycount, times); System.out.println( ʺmyCount.count = ʺ + mycount.count); System.out.println(ʺtimes = ʺ+ times); public static void increment(count c, int times) { c.count++; times++; class Count { int count; Count(int c) { count = c; Count() { count = 1; a) 99 b) 100 c) 101 d) 98 33. Assume s is ʺABCABCʺ, the method returns a new string ʺaBCaBCʺ. a) s.replace(ʺabcabcʺ, ʺaBCaBCʺ) b) s.replace(ʹaʹ, ʹAʹ) c) s.tolowercase(s) d) s.replace(ʹaʹ, ʹaʹ) e) s.tolowercase() 34. Suppose Character x = new Character(ʹaʹ), returns true. (Choose all that apply.) a) x.comparetoignorecase(ʹaʹ) b) x.equals(ʹaʹ) c) x.equals(ʺaʺ) d) x.equalsignorecase(ʹaʹ) e) x.equals(new Character(ʹaʹ)) 35. Analyze the following code: (Choose all that apply.) A a = new A(); a.print(); class A { String s; A(String s) { this.s = s; void print() { System.out.println(s); a) The program would compile and run if you change A a = new A() to A a = new A(ʺ5ʺ). b) The program has a compilation error because class A does not have a default constructor. c) The program compiles and runs fine and prints nothing. d) The program has a compilation error because class A is not a public class. 36. is invoked to create an object. a) A method with a return type b) The main method c) A constructor d) A method with the void return type 5

37. Analyze the following code and choose the best answer: public class Foo { private int x; Foo foo = new Foo(); System.out.println(foo.x); a) Since x is private, it cannot be accessed from an object foo. b) You cannot create a self-referenced object; that is, foo is created inside the class Foo. c) Since x is an instance variable, it cannot be directly used inside a main method. However, it can be accessed through an object such as foo in this code. d) Since x is defined in the class Foo, it can be accessed by any method inside the class without using an object. You can write the code to access x without creating an object such as foo in this code. 38. Which of the following is the correct header of the main method? (Choose all that apply.) a) public static void main(string x[ ]) b) public static void main(string[ ] args) c) public static void main(string args[ ]) d) static void main(string[ ] args) e) public static void main(string[ ] x) 39. Which of the following statements creates an instance of File on Window for the file c:\t.txt? a) new File(ʺc://txt.txtʺ) b) new File(ʺc:/txt.txtʺ) c) new File(ʺc:\\txt.txtʺ) d) new File(ʺc:\txt.txtʺ) 40. Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following methods will cause the list to become [Beijing, Chicago, Singapore]? a) x.add(ʺchicagoʺ) b) x.add(1, ʺChicagoʺ) c) x.add(0, ʺChicagoʺ) d) x.add(2, ʺChicagoʺ) 41. What is the output of the following code? String s1 = ʺWelcome to Java!ʺ; String s2 = s1; if (s1 == s2) System.out.println(ʺs1 and s2 reference to the same String objectʺ); else System.out.println(ʺs1 and s2 reference to different String objectsʺ); a) s1 and s2 reference to different String objects b) s1 and s2 reference to the same String object 42. Analyze the following code: (Choose all that apply.) Object a1 = new A(); Object a2 = new Object(); System.out.println(a1); System.out.println(a2); class A { int x; public String tostring() { return ʺAʹs x is ʺ + x; a) When executing System.out.println(a2), the tostring() method in the Object class is invoked. b) The program cannot be compiled, because System.out.println(a1) is wrong and it should be replaced by System.out.println(a1.toString()); c) When executing System.out.println(a1), the tostring() method in the Object class is invoked. d) When executing System.out.println(a1), the tostring() method in the A class is invoked. 6

43. Suppose ArrayList x contains two strings [Beijing, Singapore]. Which of the following method will cause runtime errors? (Choose all that apply.) a) x.remove(2) b) x.get(2) c) x.set(2, ʺNew Yorkʺ); d) x.get(1) e) x.size() 44. You can create an ArrayList using. a) ArrayList() b) new ArrayList() c) new ArrayList[ ] d) new ArrayList[100] 45. Which of the following statements are true? (Choose all that apply.) a) A method may be implemented in several subclasses. The Java Virtual Machine dynamically binds the implementation of the method at runtime. b) You can always pass an instance of a subclass to a parameter of its superclass type. This feature is known as polymorphism. c) The compiler finds a matching method according to parameter type, number of parameters, and order of the parameters at compilation time. d) Dynamic binding can apply to static methods. e) Dynamic binding can apply to instance methods. 46. Which of the following statements are true? (Choose all that apply.) a) A method can be overridden in the same class. b) If a method overrides another method, these two methods must have the same signature. c) If a method overloads another method, these two methods must have the same signature. d) A method can be overloaded in the same class. 47. To create an instance of BigDecimal for 454.45, use a) new BigDecimal(ʺ454.45ʺ); b) BigInteger(454.45); c) BigInteger(ʺ454.45ʺ); d) new BigInteger(454.45); 48. Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct? (Choose all that apply.) a) A a = new A(); b) B b = new B(); c) A a = new B(); d) B b = new A(); 49. What is the output of the following code: Object o1 = new Object(); Object o2 = new Object(); System.out.print((o1 == o2) + ʺ ʺ + (o1.equals(o2))); a) false true b) false false c) true true d) true false 50. Object-oriented programming allows you to derive new classes from existing classes. This is called. a) generalization b) abstraction c) encapsulation d) inheritance 51. Analyze the following code: (Choose all that apply.) ArrayList list = new ArrayList(); list.add(ʺbeijingʺ); list.add(ʺtokyoʺ); list.add(ʺshanghaiʺ); list.set(3, ʺHong Kongʺ); a) The last line in the code causes a runtime error because there is no element at index 3 in the array list. b) If you replace the last line by list.add(4, ʺHong Kongʺ), the code will compile and run fine. c) If you replace the last line by list.add(3, ʺHong Kongʺ), the code will compile and run fine. d) The last line in the code has a compile error because there is no element at index 3 in the array list. 7

52. Assume Calendar calendar = new GregorianCalendar(). returns the number of days in a month. a) calendar.get(calendar.week_of_year) b) calendar.getactualmaximum(calendar.day_of_month) c) calendar.get(calendar.month_of_year) d) calendar.get(calendar.month) e) calendar.get(calendar.week_of_month) 53. Given the following code, find the compile error. (Choose all that apply.) m(new GraduateStudent()); m(new Student()); m(new Person()); m(new Object()); public static void m(student x) { System.out.println(x.toString()); class GraduateStudent extends Student { class Student extends Person { public String tostring() { return ʺStudentʺ; class Person extends Object { public String tostring() { return ʺPersonʺ; a) m(new Person()) causes an error b) m(new Object()) causes an error c) m(new Student()) causes an error d) m(new GraduateStudent()) causes an error 54. Suppose A is an interface, B is a concrete class with a default constructor that implements A. Which of the following is correct? (Choose all that apply.) a) A a = new B(); b) B b = new B(); c) B b = new A(); d) A a = new A(); 55. is a reference type. (Choose all that apply.) a) An interface type b) A class type c) A primitive type d) An array type 56. Which statements are most accurate regarding the following classes? class A { private int i; protected int j; class B extends A { private int k; protected int m; a) An object of B contains data fields j, m. b) An object of B contains data fields k, m. c) An object of B contains data fields i, j, k, m. d) An object of B contains data fields j, k, m. 57. Which of the following statements are true? (Choose all that apply.) a) If you compile an interface without errors, a.class file is created for the interface. b) If you compile an interface without errors, but with warnings, a.class file is created for the interface. c) If you compile a class without errors but with warnings, a.class file is created. d) If you compile a class with errors, a.class file is created for the class. 58. is a reference type. (Choose all that apply.) a) An array type b) A primitive type c) An interface type d) A class type 59. Which of the following classes are immutable? (Choose all that apply.) a) Double b) BigDecimal c) String d) Integer e) BigInteger 60. To assign a double variable d to a float variable x, you write a) x = (int)d; b) x = d; c) x = (long)d d) x = (float)d; 8

Answer Key Testname: JP2011MIDTERMTESTV2 1. e 2. d 3. a 4. d 5. a 6. b 7. c 8. e 9. b 10. b 11. a 12. d 13. b 14. b 15. b 16. a 17. a, b, c 18. a 19. d 20. a 21. a 22. d 23. a 24. a, b 25. a 26. e 27. b 28. a 29. c 30. a 31. b 32. c 33. d 34. b, e 35. a, b 36. c 37. c 38. a, b, c, e 39. c 40. b 41. b 42. a, d 43. a, b, c 44. b 45. a, b, c, e 46. b, d 47. a 48. b, c 49. b 50. d 51. a, c 52. b 53. a, b 54. a, b 55. a, b, d 56. c 57. a, b, c 58. a, c, d 59. a, b, c, d, e 60. d 9

1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 10