Chapter 1: Introduction to Computers, Programs, and Java

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

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

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

Questions Answer Key Questions Answer Key Questions Answer Key

Practice Questions for Chapter 9

Computer Programming, I. Laboratory Manual. Final Exam Solution

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

Pull Lecture Materials and Open PollEv. Poll Everywhere: pollev.com/comp110. Lecture 12. else-if and while loops. Once in a while

CS1150 Principles of Computer Science Loops (Part II)

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Full file at

Date: Dr. Essam Halim

Inheritance and Polymorphism

Questions Answer Key Questions Answer Key Questions Answer Key

Loops. CSE 114, Computer Science 1 Stony Brook University

Zheng-Liang Lu Java Programming 45 / 79

Introduction to Programming Using Java (98-388)

CSE 8B Final Exam Fall 2015

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

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

Review for Test 1 (Chapter 1-5)

CS 113 PRACTICE FINAL

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

School of Computer Science CPS109 Course Notes 6 Alexander Ferworn Updated Fall 15. CPS109 Course Notes 6. Alexander Ferworn

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS260 Intro to Java & Android 03.Java Language Basics

Inheritance and Polymorphism

CS 112 Programming 2. Lecture 06. Inheritance & Polymorphism (1) Chapter 11 Inheritance and Polymorphism

Lecture 1 Java SE Programming

Java Fundamentals (II)

CS2401 QUIZ 5 February 26, questions / 20 points / 20 minutes NAME:..

Programming - 2. Common Errors

Practice for Chapter 11

COMPUTER APPLICATIONS

Lecture Notes Chapter #9_b Inheritance & Polymorphism

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

Java Programming Language. 0 A history

Lara Technologies Special-Six Test

Answer1. Features of Java

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

3chapter C ONTROL S TATEMENTS. Objectives

Full download all chapters instantly please go to Solutions Manual, Test Bank site: testbanklive.com

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

Introduction to Java

Java Identifiers, Data Types & Variables

Selected Questions from by Nageshwara Rao

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

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

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Constructor. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Computer Science II (20073) Week 1: Review and Inheritance

Following is the general form of a typical decision making structure found in most of the programming languages:

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

Questions Answer Key Questions Answer Key Questions Answer Key

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

( &% class MyClass { }

Computer Science II (20082) Week 1: Review and Inheritance

University of Palestine. Mid Exam Total Grade: 100

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

PROGRAMMING FUNDAMENTALS

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

A506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Lec 3. Compilers, Debugging, Hello World, and Variables

Computer Science E-119 Fall Problem Set 1. Due before lecture on Wednesday, September 26

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

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

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

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

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Java Bytecode (binary file)

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Creating Classes and Objects

Class, Variable, Constructor, Object, Method Questions

Pace University. Fundamental Concepts of CS121 1

Math Modeling in Java: An S-I Compartment Model

Java Primer 1: Types, Classes and Operators

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003

Controls Structure for Repetition

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Chapter 3 Selection Statements

Transcription:

Chapter 1: Introduction to Computers, Programs, and Java 1. Q: When you compile your program, you receive an error as follows: 2. 3. %javac Welcome.java 4. javac not found 5. 6. What is wrong? 7. A: Two possible reasons: 1. Java is not installed on your system; or 2. Java is installed, but the environment variable PATH is not set. To set the path correctly, see Section 1.3 in Supplement I.B. 8. Q: When you compile your program, you receive an error as follows: 9. 10. %javac Welcome.java 11. javac: file not found: Welcome.java 12. Usage: javac 13. 14. use -help for a list of possible options 15. 16. What is wrong? 17. A: File Welcome.java does not exist in the directory. Check if Welcome.java is in the current directory. 18. Q: When you run your program, you receive an error as follows: 19. 20. %java Welcome 21. Exception in thread "main" java.lang.noclassdeffounderror: Welcome 22. 23. What is wrong? 24. A: File Welcome.class does not exist. You have to first compile Welcome.java to produce Welcome.class in the current directory. 25. Q: When you run your program, you receive an error as follows: 26. 27. %java Welcome 28. Exception in thread "main" java.lang.nosuchmethoderror: main 29. 30. What is wrong? 31. A: Please check if you have a main method in Welcome.java or you have spelled the method public static void main(string[] args) correctly. 32. Q: When you compile your program, you receive an error as follows: 33. 34. %javac Welcome.java 35. Welcome.java:2: cannot find symbol 36. symbol : class string 37. location: class Welcome 38. public static void main(string[] args) { 39. ^ 40. 41. 1 error 42. 43. What is wrong? 44. A: Java is case-sensitive. string should be spelled as String. Chapter 2: Elementary Programming

1. Q: What is wrong in the following code? 2. 3. public class Test { 4. public static void main(string[] args) { 5. i = 1; 6. j = i * i * i; 7. } 8. } 9. A: You will get a compile error that indicates symbol i cannot be resolved. Every variable must be declared before being used. 10. Q: What is wrong in the following code? 11. 12. 1 public class Test { 13. 2 public static void main(string[] args) { 14. 3 int i; 15. 4 j = i * i * i; 16. 5 } 17. 6 } 18. 19. A: You will get a compile error that indicates variable i is not initialized in line 3. 20. Q: What is wrong in the following code? 21. 22. 1 public class Test { 23. 2 public static void main(string[] args) { 24. 3 int i; 25. 4 int j = 5; 26. 5 System.out.println(j * 4.5); 27. 6 } 28. 7 } 29. A: The program compiles and runs fine. However, i is never used. This might be a potential programming error. Most IDE now can alert you of this situation. 30. Q: Why does this program display 0? 31. 1 public class Test { 32. 2 public static void main(string[] args) { 33. 3 int i = 9; 34. 4 int j = 5 / 6 * (i * 4); 35. 5 System.out.println(j); 36. 6 } 37. 7 } 38. A: This type of mistakes is very common for my students. We have covered this repeatedly in the text. Here just to remind you again 5 / 6 is 0, because both 5 and 6 are integers. 39. Q: What is the output of this program? 40. public class Test { 41. public static void main(string[] args) { 42. System.out.println(2147483647 + 1); 43. } 44. }

45. A: Run this program. You will see the output is -2147483648. Why? 2147483647 is the largest int value. Adding 1 to this value causes overflow. It is not a runtime error in Java. 46. Q: What is the output of the following code? 47. 1 public class Test { 48. 2 public static void main(string[] args) { 49. 3 System.out.println("1 + 2 is " + i + j); 50. 4 } 51. 5 } 52. A: The program displays 1 + 2 is 12. If you want the output to be 1 + 2 is 3, put i + j inside the parenthese (i + j) to force i + j to be evaluated first. 53. Q: What is the output of this program? 54. 55. public class Test { 56. public static void main(string[] args) { 57. char ch = 'a'; 58. System.out.println('a' + 1); 59. System.out.println(++ch); 60. } 61. } 62. A: for 'a' + 1, 'a' is converted into int. so 'a' + 1 is 97 + 1 = 98. For ++ch, the result is 'b'. Chapter 3: Selections 1. Q: What is wrong in the following code? 2. 1 public class Test { 3. 2 public static void main(string[] args) { 4. 3 int i, j, k; 5. 4 i = j = k = 3; 6. 5 if (i < j < k) 7. 6 System.out.println("i, j, and k are in increasing order"); 8. 7 } 9. 8 } 10. A: i < j evaluates to a boolean value, which cannot be compared with k. The correct expression should be (i < j && j < k). 11. Q: What is the output of the following code? 12. public class Test { 13. public static void main(string[] args) { 14. System.out.println(5.0-1.0-1.0-1.0-1.0-1.0 == 5.0); 15. } 16. } 17. A: Run the program. You will be surprised to see the output is false. Why? because floating-point numbers are not stored in complete accuracy. You are dealing with approximation for floating-point numbers. Don't compare two floating-point numbers using equality == operators. 18. Q: What is the output of the following code? 19. public class Test { 20. public static void main(string[] args) {

21. boolean done = false; 22. if (done = true) { 23. System.out.println("done"); 24. } 25. } 26. } 27. A: Run the program. You will see the output is done. Why? you assign true to done. So, the condition is true. To check whether done is true, you should use done == true, or simply 28. if (done) { 29. System.out.println("done"); 30. } 31. Q: What is the output of the following code? 32. public class Test { 33. public static void main(string[] args) { 34. boolean done = false; 35. if (done); 36. { 37. System.out.println("done"); 38. } 3 40. } 41. A: Run the program. You will see the output is done. Why? because the if statement ends with 42. if (done); The semicolon ends the if statement. The lines { } System.out.println("done"); are not part of the if statement. 43. Q: What is the output of the following code? 44. public class Test { 45. public static void main(string[] args) { 46. char ch = 'a'; 47. switch (ch) { 48. case: 'a': System.out.print(ch); 49. case: 'b': System.out.print(ch); 50. case: 'c': System.out.print(ch); 51. } 52. } 53. } 54. A: Run the program. You will see the output is aaa. Why? There is no break statement for the case statement. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. This phenomenon is referred to as the fall-through behavior. Chapter 4: Loops 1. Q: What is wrong in the following code? 2. public class Test { 3. public static void main(string[] args) { 4. int i = 0; 5. while (i < 10) {

6. System.out.println("Welcome to Java!"); 7. } 8. } 10. A: i is always 0. This is an infinite loop. This is a common error for while and do-while loops. Make sure the control variable changes to cause the loop to stop eventually. 11. Q: What is wrong in the following code? 12. public class Test { 13. public static void main(string[] args) { 14. int i = 0; 15. while (i < 10) 16. System.out.println("Welcome to Java!"); 17. i++; 18. } 1 20. 21. A: i++ is not in the body of the while loop. So, this is an infinite loop. If the loop body contains more than one statement, use the braces to group them. Chapter 5: Methods 1. Q: What is the output of the following code? 2. public class Test { 3. public static void main(string[] args) { 4. int x = 1; 5. p(x); 6. System.out.println("x is " + x); 7. } 8. 9. public static void p(int x) { 10. x = x + 1; 11. } 12. } 13. A: x is 1. Invoking p(x), the value of x is passed to the parameter in the method. Variable x defined in the main method and x defined in method p are two independent variables. 14. Q: Does Math.sin(90) return the sine value for angle 90 degree? 15. A: No. To return the sine value for angle 90 degree, use Math.sin(Math.PI / 2). Chapter 6: Single-Dimensional Arrays 1. Q: What is wrong in the following code? 2. public class Test { 3. public static void main(string[] args) { 4. int[] x = new int[10]; 5. 6. for (int i = 0; i <= x.length; i++) 7. System.out.print(x[i]);

8. } 10. A: This is a common error, known as off-by-one. Here the array index is from 0 to 9, but the last i in the loop will be x.length (10). At 10, the index is out of range. 11. Q: What is wrong the following code? 12. public class Test { 13. public static void main(string[] args) { 14. int[] x = new int[10]; 15. p(x); 16. System.out.print(x[0]); 17. } 18. 19. public static void p(int[] list) { 20. list[0] = 10; 21. } 22. } 23. A: The output is 10. When invoking p(x), the reference value of array x is passed to list. The first element in the array is changed in the method. Chapter 8: Objects and Classes 1. Q: Do local variables have default values? Do instance variables have default values? Do static variables have default values? A: Local variables don't have default values. Instance and static variables are data fields and have default values. 2. Q: What is the printout of the following code? 3. public class Test { 4. public void Test() { 5. System.out.println("Test"); 6. } 7. public static void main(string[] args) { 8. Test t = new Test(); 10. } 11. A: None. Note that "public void Test()" is not a constructor. It is a method. The class Test has a default constructor. 12. Q: What is wrong the following code? 13. public class Test { 14. public static void main(string[] args) { 15. String[] x = new String[10]; 16. System.out.print(x[0].toString());

17. } 18. } 19. A: When you create an array of objects. Each element of the array has a null value. So, invoking x[0].tostring() causes a NullPointerException. Chapter 11: Inheritance and Polymorphism 1. Q: What is wrong the following code? 2. public class Apple extends Fruit { 3. } 4. 5. class Fruit { 6. public Fruit(String name) { 7. System.out.println("Fruit's constructor is invoked"); 8. } A: Since no constructor is explicitly defined in Apple, Apple s default no-arg constructor is defined implicitly. Since Apple is a subclass of Fruit, Apple s default constructor automatically invokes Fruit s no-arg constructor. However, Fruit does not have a no-arg constructor, because Fruit has an explicit constructor defined. Therefore, the program cannot be compiled. 10. Q: What is the difference between == and equals()? A: The == comparison operator is used for comparing two primitive data type values or for determining whether two objects have the same references. The equals method is intended to test whether two objects have the same contents, provided that the method is overridden in the defining class of the objects. The == operator is stronger than the equals method, in that the == operator checks whether the two reference variables refer to the same object. Chapter 20: Recursion 1. Q: What is wrong the following code? 2. public static long factorial(int n) { 3. return n * factorial(n - 1); 4. } A: There is no stopping condition. The method runs infinitely. 5. Q: What is wrong the following code? 6. public class Test { 7. public static void main(string[] args) { 8. printline(2, "Welcome to Java"); 10. 11. public static void printline(int n, String message) { 12. while (n > 1) { 13. System.out.println(message); 14. printline(n - 1, message); 15. } 16. } 17. }

A: while should be replaced by if. With "while", the method runs infinitely. To understand why this happens, draw a stack. First the main method is placed into the stack. Second, printline(2, "Welcome to Java") is placed into the stack. Now n is 2. Third, printline(1, "Welcome to Java") is placed into the stack. Now n is 1, so the method finished and popped out of the stack. But now you are still in the second method call. In the second method call, n is still 2. The loop-continuationcondition is still true. So, the loop continues. The method will run forever.