Chapter 2: Basic Elements of Java

Similar documents
Full file at

1. In C++, reserved words are the same as predefined identifiers. a. True

Chapter 2: Using Data

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Computational Expression

4. Inputting data or messages to a function is called passing data to the function.

I. Variables and Data Type week 3

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 02: Using Data

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

Chapter 2: Using Data

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

Primitive Data Types: Intro

CEN 414 Java Programming

Chapter 2: Data and Expressions

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

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

Programming with Java

Peer Instruction 1. Elementary Programming

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

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

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

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Elementary Programming

Introduction to C# Applications

Full file at

JAVA Programming Fundamentals

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

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

Chapter 2: Data and Expressions

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

1. An operation in which an overall value is computed incrementally, often using a loop.

Objectives. In this chapter, you will:

Building Java Programs

Lecture Set 2: Starting Java

Chapter 2. Elementary Programming

Full file at

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Topic 11 Scanner object, conditional execution

Lecture Set 2: Starting Java

Java Programming Language. 0 A history

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Computer Components. Software{ User Programs. Operating System. Hardware

Using Java Classes Fall 2018 Margaret Reid-Miller

Object Oriented Programming. Java-Lecture 1

3. Java - Language Constructs I

BASIC ELEMENTS OF A COMPUTER PROGRAM

Chapter 3. Selections

Introduction to Java Applications

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Basics of Java Programming

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

JAVA Ch. 4. Variables and Constants Lawrenceville Press

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

Input. Scanner keyboard = new Scanner(System.in); String name;

Building Java Programs

Computer Components. Software{ User Programs. Operating System. Hardware

B.V. Patel Institute of BMC & IT, UTU 2014

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

Java Console Input/Output The Basics. CGS 3416 Spring 2018

Course Outline. Introduction to java

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

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

Introduction to Programming Using Java (98-388)

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Java Notes. 10th ICSE. Saravanan Ganesh

Full file at

Introduction to Java Applications; Input/Output and Operators

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

More Programming Constructs -- Introduction

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Chapter 2 Basic Elements of C++

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Chapter-8 DATA TYPES. Introduction. Variable:

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

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

UNIT- 3 Introduction to C++

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Chapter 2 ELEMENTARY PROGRAMMING

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Data Conversion & Scanner Class

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

C++ Final Exam 2017/2018

Differentiate Between Keywords and Identifiers

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS 112 Introduction to Programming

4 WORKING WITH DATA TYPES AND OPERATIONS

Example: Tax year 2000

Welcome to the Primitives and Expressions Lab!

CS242 COMPUTER PROGRAMMING

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

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

Transcription:

Chapter 2: Basic Elements of Java TRUE/FALSE 1. The pair of characters // is used for single line comments. ANS: T PTS: 1 REF: 29 2. The == characters are a special symbol in Java. ANS: T PTS: 1 REF: 30 3. An identifier can be any sequence of characters and integers. ANS: F PTS: 1 REF: 31 4. The symbol '5' does not belong to the char data type because 5 is a digit. ANS: F PTS: 1 REF: 33 5. The data type float is a floating-point data type. ANS: T PTS: 1 REF: 35 6. The number of significant digits in a double variable is up to 15. ANS: T PTS: 1 REF: 35 7. A value such as 'd' is called a character constant. ANS: T PTS: 1 REF: 36 8. An operator that has only one operand is called a unique operator. ANS: F PTS: 1 REF: 36 9. Operators of the same precedence are evaluated from right to left. ANS: F PTS: 1 REF: 39 10. When evaluating a mixed expression, all integer operands are converted to floating-point numbers with the decimal part of zero. ANS: T PTS: 1 REF: 41 11. When a value of one data type is automatically changed to another data type, an implicit type coercion has occurred. ANS: T PTS: 1 REF: 43 12. Suppose x = 18.6. The output of the statement System.out.println((int)(x) / 3); is 6.

ANS: T PTS: 1 REF: 44 13. The null string contains only the blank character. ANS: F PTS: 1 REF: 45 14. The value of a variable may change during program execution. ANS: T PTS: 1 REF: 50 15. If a = 4; and b = 3;, then after the statement a = b; executes, the value of b is 4 and the value of a is 3. ANS: F PTS: 1 REF: 51 16. The Java language is strongly typed. ANS: T PTS: 1 REF: 54 17. Java automatically initializes all variables. ANS: F PTS: 1 REF: 55 18. Suppose console is a Scanner object initialized with the standard input device. The expression console.nextint(); is used to read one int value and the expression console.nextdouble(); is used to read two int values. ANS: F PTS: 1 REF: 56 19. Suppose console is a Scanner object initialized with the standard input device and feet and inches are int variables. Consider the following statements: feet = console.nextint(); inches = console.nextint(); These statements require the value of feet and inches to be input on separate lines. ANS: F PTS: 1 REF: 58 20. Suppose that index is an int variable. The statement index = index + 1; is equivalent to index++; ANS: T PTS: 1 REF: 64 21. If ++x is used in an expression, first the expression is evaluated, and then the value of x is incremented by 1. ANS: F PTS: 1 REF: 65 22. Suppose x = 8. After the execution of the statement y = x++; y is 8 and x is 10. ANS: F PTS: 1 REF: 65

23. Both System.out.println and System.out.print can be used to output a string on the standard output device. ANS: T PTS: 1 REF: 66 24. The character sequence \n moves the insertion point at the end of the current line. ANS: F PTS: 1 REF: 67 25. In Java, a period is used to terminate a statement. ANS: F PTS: 1 REF: 81 MULTIPLE CHOICE 1. The rules of a programming language tell you which statements are legal, or accepted by the programming language. a. semantic c. syntax b. logical d. grammatical ANS: C PTS: 1 REF: 29 2. Which of the following is NOT a reserved word in Java? a. double c. static b. throws d. num ANS: D PTS: 1 REF: 30 3. Which of the following is a valid Java identifier? a. $pay c. newgrade! b. 4myGrade! d. 1dollar ANS: A PTS: 1 REF: 31 4. Which of the following is a valid int value? a. 3279 c. 3270.00 b. 3,279 d. -922337203684547758808 ANS: A PTS: 1 REF: 33 5. Which of the following is a valid char value? a. '$_' c. 'n\' b. '%' d. a ANS: B PTS: 1 REF: 33-34 6. The memory allocated for a double value is bytes. a. 2 c. 8 b. 4 d. 16 ANS: C PTS: 1 REF: 35 7. The value of the expression 14 % 3 is. a. 1 c. 3

b. 2 d. 4 ANS: B PTS: 1 REF: 37 8. Operators that have two operands are called. a. unary operators c. operators b. binary operators d. expressions ANS: B PTS: 1 REF: 36 9. The value of the expression 5 + 10 % 4-3 is. a. 0 c. 4 b. 2 d. 5 ANS: C PTS: 1 REF: 39 10. The expression (int)8.7 evaluates to. a. 8 c. 9.0 b. 8.0 d. 9 ANS: A PTS: 1 REF: 43 11. The expression (double)(5 + 4) evaluates to. a. 8 c. 9.0 b. 9 d. 10.0 ANS: C PTS: 1 REF: 43 12. The length of the string "first java program" is: a. 16 c. 19 b. 18 d. 20 ANS: B PTS: 1 REF: 45 13. Which of the following statements about a named constant is NOT true? a. Its content cannot change during program execution. b. Its value can be changed during program execution. c. It is a memory location. d. It is declared using the reserved word final. ANS: B PTS: 1 REF: 48 14. What type of Java statement(s) stores a value in a variable? a. input b. output c. assignment d. Both an input statement and an assignment statement ANS: D PTS: 1 REF: 51 15. Suppose that x and y are int variables and x = 7 and y = 8. After the statement: x = x * y - 2; executes, the value of x is. a. 42 c. 56 b. 54 d. 58 ANS: B PTS: 1 REF: 51

16. Given char ch; int num; double pay; Which of the following assignment statements are valid? (i) ch = '*'; (ii) pay = num * pay; (iii) rate * 40 = pay; a. Only (i) is valid c. (ii) and (iii) are valid b. (i) and (ii) are valid d. (i) and (iii) are valid ANS: B PTS: 1 REF: 51 17. Suppose that alpha and beta are int variables. The statement alpha = --beta; is equivalent to the statement(s). a. beta = beta - 1; c. beta = beta - 1; alpha = 1 - beta; alpha = beta; b. beta = beta - 1; d. alpha = beta; alpha = beta - 1; beta = beta - 1; ANS: C PTS: 1 REF: 64-65 18. The standard output object in Java is. a. output c. Sys.out b. System.out d. System.in ANS: B PTS: 1 REF: 66 19. What is the output of the following statement? System.out.println("Welcome \n Home"); a. WelcomeHome b. Welcome Home c. Welcome Home d. Welcome \n Home ANS: C PTS: 1 REF: 67 20. Which of the following is the newline character? a. \r c. \l b. \n d. \b ANS: B PTS: 1 REF: 70 21. Consider the following program. public class CircleArea

static Scanner console = new Scanner(System.in); static final float PI = 3.14; public static void main(string[]args) float r; float area; r = console.nextdouble(); area = PI * r * r; System.out.println("Area = " + area); To successfully compile this program, which of the following import statement is required? a. import java.io; c. import java.lang; b. import java.util; d. No import statement is required ANS: B PTS: 1 REF: 71-72 22. Consider the following program. // Insertion Point 1 public class CircleArea // Insertion Point 2 static final float PI = 3.14 public static void main(string[]args) //Insertion Point 3 float r = 2.0; float area; area = PI * r * r; System.out.println("Area = " + area); // Insertion Point 4 In the above code, where do the import statements belong? a. Insertion Point 1 c. Insertion Point 3 b. Insertion Point 2 d. Insertion Point 4 ANS: A PTS: 1 REF: 75 23. are executable statements that inform the user what to do. a. Variables c. Named constants b. Prompt lines d. Expressions ANS: B PTS: 1 REF: 81 24. The declaration int a, b, c; is equivalent to which of the following? a. int a, b c; c. int abc; b. int a; d. int a b c; int b;

int c; ANS: B PTS: 1 REF: 82 25. Suppose x = 4 and y = 2. If the statement x *= y; is executed once, what is the value of x? a. 2 c. 8 b. 4 d. This is an illegal statement in Java. ANS: C PTS: 1 REF: 85