Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

Similar documents
Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

4. If the following Java statements are executed, what will be displayed?

Full file at

Computational Expression

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

Datatypes, Variables, and Operations

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

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

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

Getting started with Java

Elementary Programming

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CHAPTER 2 Java Fundamentals

Chapter 2: Java Fundamentals

8/23/2014. Chapter Topics. Chapter Topics (2) Parts of a Java Program. Parts of a Java Program. Analyzing The Example. Chapter 2: Java Fundamentals

Full file at

Peer Instruction 1. Elementary Programming

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

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

Programming with Java

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

Java Basic Programming Constructs

Oct Decision Structures cont d

CMSC131. Introduction to your Introduction to Java. Why Java?

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

Fundamentals of Programming Data Types & Methods

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

Key Differences Between Python and Java

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

Lecture Set 2: Starting Java

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

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

CS 302: Introduction to Programming

Lecture Set 2: Starting Java

Object-Oriented Programming

LECTURE 02 INTRODUCTION TO C++

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Decisions in Java IF Statements

Java Bytecode (binary file)

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

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

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

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Chapter 2 Elementary Programming

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

Introduction to Java & Fundamental Data Types

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Java Identifiers, Data Types & Variables

MODULE 02: BASIC COMPUTATION IN JAVA

Lecture 2 Tao Wang 1

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

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Basics of Java Programming

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

Basic Computation. Chapter 2

DATA TYPES AND EXPRESSIONS

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

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

4 Programming Fundamentals. Introduction to Programming 1 1

Full file at

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

CEN 414 Java Programming

Chapter 2 ELEMENTARY PROGRAMMING

CP122 Computer Science I. Chapter 2: Data Types, Variables, and I/O

Introduction to Programming Using Java (98-388)

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

Software and Programming 1

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

The C++ Language. Arizona State University 1

Chapter 2 Primitive Data Types and Operations. Objectives

PROGRAMMING FUNDAMENTALS

Ex: If you use a program to record sales, you will want to remember data:

Date: Dr. Essam Halim

Values and Variables 1 / 30

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

Chapter 2 Primitive Data Types and Operations

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Chapter 2: Programming Concepts

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Selected Questions from by Nageshwara Rao

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Important Java terminology

Chapter 02: Using Data

Full file at

Array. Prepared By - Rifat Shahriyar

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

CS1150 Principles of Computer Science Math Functions, Characters and Strings (Part II)

JAVA Programming Fundamentals

Conditional Execution

CS11 Java. Fall Lecture 1

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

CHAPTER 3 BASIC INSTRUCTION OF C++

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

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Chapter 4: Control Structures I

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Transcription:

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals 2.1 Multiple Choice Questions 1) Which one of the following would contain the translated Java byte code for a program named Demo? A) Demo.java B) Demo.code C) Demo.class D) Demo.byte 2) To compile a program named First, use the following command: A) java First.java B) javac First C) javac First.java D) compile First.javac 3) A Java program must have at least one of these: A) Class definition B) Variable C) Comment D) System.out.println(); statement 4) In Java, the beginning of a comment is marked with: A) // B) "" C) ; D) # 5) The term typically refers to the device that displays console output. A) standard output device B) central processing unit C) secondary storage device D) liquid crystal display 6) In Java, must be declared before they can be used. A) variables B) literals C) key words D) comments 1

7) If the following Java statements are executed, what will be displayed? System.out.println("The top three winners are\n"); System.out.print("Jody, the Giant\n"); System.out.print("Buffy, the Barbarian"); System.out.println("Adelle, the Alligator"); A) The top three winners are Jody, the Giant Buffy, the Barbarian Adelle, the Alligator B) The top three winners are Jody, the Giant\nBuffy, the BarbarianAdelle, the Alligator C) The top three winners are Jody, the Giant\nBuffy, the BarbarianAdelle, and the Albino D) The top three winners are Jody, the Giant Buffy, the BarbarianAdelle, the Alligator 8) This is a value that is written into the code of a program. A) literal B) assignment statement C) variable D) operator 9) When the + operator is used with strings, it is known as the: A) Assignment operator B) String concatenation operator C) Addition operator D) Combined assignment operator 10) What would be printed out as a result of the following code? System.out.println("The quick brown fox" + "jumped over the \n" "slow moving hen."); A) The quick brown fox jumped over the \nslow moving hen. B) The quick brown fox jumped over the slow moving hen. C) The quick brown fox jumped over the slow moving hen. D) Nothing. This is an error. 2

11) Which of the following is NOT a rule that must be followed when naming identifiers? A) The first character must be one of the letters a-z, A-Z, and underscore or a dollar sign. B) Identifiers can contain spaces. C) Uppercase and lowercase characters are distinct. D) After the first character, you may use the letters a-z, A-Z, the underscore, a dollar sign, or digits 0-9. 12) Which of the following cannot be used as identifiers in Java? A) Variable names B) Class names C) Key words D) All of the above E) None of the above 13) In Java, it is standard practice to capitalize the first letter of: A) Class names B) Variable names C) Key words D) Literals 14) Which of the following is NOT a primitive data type? A) short B) long C) float D) String 15) Which of the following is valid? A) float y; y = 54.9; B) float y; double z; z = 934.21; y = z; C) float w; w = 1.0f; D) float v; v = 1.0; 16) The boolean data type may contain values in the following range of values: A) true or false B) -128 to + 127 C) - 2,147,483,648 to +2,147,483,647 D) - 32,768 to +32,767 3

17) Character literals are enclosed in ; string literals are enclosed in. A) single quotes; single quotes B) double quotes; double quotes C) single quotes; double quotes D) double quotes; single quotes 18) What is the result of the following expression? 10 + 5 * 3-20 A) -5 B) 5 C) 25 D) -50 19) What is the result of the following expression? 25 / 4 + 4 * 10 % 3 A) 19 B) 5.25 C) 3 D) 7 20) What will be displayed as a result of executing the following code? int x = 5, y = 20; x += 32; y /= 4; System.out.println("x = " + x + ", y = " + y); A) x = 32, y = 4 B) x = 9, y = 52 C) x = 37, y = 5 D) x = 160, y = 80 21) What will be the value of z as a result of executing the following code? int x = 5, y = 28; float z; z = (float) (y / x); A) 5.60 B) 5.6 C) 3.0 D) 5.0 4

22) What will be the displayed when the following code is executed? final int x = 22, y = 4; y += x; System.out.println("x = " + x + ", y = " + y); A) x = 22, y = 4 B) x = 22, y = 26 C) x = 22, y = 88 D) Nothing, this is an error 23) In the following Java statement what value is stored in the variable name? String name = "John Doe"; A) John Doe B) The memory address where "John Doe" is located C) name D) The memory address where name is located 5

24) What will be displayed as a result of executing the following code? int x = 6; String msg = "I am enjoying this class."; String msg1 = msg.touppercase(); String msg2 = msg.tolowercase(); char ltr = msg.charat(x); int strsize = msg.length(); System.out.println(msg); System.out.println(msg1); System.out.println(msg2); System.out.println("Character at index x = " + ltr); System.out.println("msg has " + strsize + "characters."); A) I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = e msg has 24 characters. B) I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = e msg has 25 characters. C) I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = n msg has 24 characters. D) I am enjoying this class. I AM ENJOYING THIS CLASS. i am enjoying this class. Character at index x = n msg has 25characters. 6

25) What will be displayed as a result of executing the following code? public class test { public static void main(string[] args) { int value1 = 9; System.out.println(value1); int value2 = 45; System.out.println(value2); System.out.println(value3); value = 16; } } A) 9 45 16 B) 94516 C) 9 45 16 D) Nothing, this is an error. 26) Which of the following is NOT a valid comment statement? A) // comment 1 B) /* comment 2 */ C) */ comment 3 /* D) /** comment 4 */ 27) When saving a Java source file, save it with an extension of: A).javac B).class C).src D).java 28) Every Java application program must have: A) a class named MAIN B) a method named main C) comments D) integer variables 29) To print "Hello, world" on the monitor, use the following Java statement: A) SystemOutPrintln("Hello, world"); B) System.out.println{"Hello, world"} C) System.out.println("Hello, world"); D) Print "Hello, world"; 7

30) To display the output on the next line, you can use the println method or use this escape sequence in the print method. A) \n B) \r C) \t D) \b 31) This is a named storage location in the computer's memory. A) Literal B) Constant C) Variable D) Operator 32) What would be displayed as a result of the following code? int x = 578; System.out.print("There are " + x + 5 + "\n" + "hens in the hen house."); A) There are 583 hens in the hen house. B) There are 5785 hens in the hen house. C) There are x5\nhens in the hen house. D) There are 5785 hens in the hen house. 33) Variables are classified according to their: A) value B) data type C) names D) location in the program 34) The primitive data types only allow a(n) to hold a single value. A) variable B) object C) class D) literal 35) If x has been declared an int, which of the following statements is invalid? A) x = 0; B) x = -58932; C) x = 1,000; D) x = 592; 8

36) Given the declaration double r;, which of the following statements is invalid? A) r = 326.75; B) r = 9.4632e15; C) r = 9.4632E15; D) r = 2.9X106; 37) Variables of the boolean data type are useful for: A) working with small integers B) evaluating true/false conditions C) working with very large integers D) evaluating scientific notation 38) What is the result of the following expression? 25-7 * 3 + 12 / 3 A) 6 B) 8 C) 10 D) 12 39) What is the result of the following expression? 17 % 3 * 2-12 + 15 A) 7 B) 8 C) 12 D) 105 40) What will be displayed after the following statements have been executed? int x = 15, y = 20, z = 32; x += 12; y /= 6; z -= 14; System.out.println("x = " + x + ", y = " + y + ", z = " +z); A) x = 27, y = 3.333, z = 18 B) x = 27, y = 2, z = 18 C) x = 27, y = 3, z = 18 D) x = 37, y = 14, z = 4 9

41) What will be the value of z after the following statements have been executed? int x = 4, y = 33; double z; z = (double) (y / x); A) 8.25 B) 4 C) 8 D) 8.0 42) This is a variable whose content is read only and cannot be changed during the program's execution. A) operator B) literal C) named constant D) reserved word 43) What will be displayed after the following statements have been executed? final double x = 99.0; x = 54.3; System.out.println("x = " + x ); A) x = 54.3 B) x C) x = 108.6 D) Nothing, this is an error. 44) Which of the following is a valid Java statement? A) String str = 'John Doe'; B) string str = "John Doe"; C) string str = 'John Doe'; D) String str = "John Doe"; 10

45) What will be displayed as a result of executing the following code? int x = 8; String msg = "I am enjoying java."; String msg1 = msg.touppercase(); String msg2 = msg.tolowercase(); char ltr = msg.charat(x); int strsize = msg.length(); System.out.println(msg); System.out.println(msg1); System.out.println(msg2); System.out.println("Character at index x = " + ltr); System.out.println("msg has " + strsize + " characters."); A) I am enjoying java. I AM ENJOYING JAVA. i am enjoying java. Character at index x = j msg has 20 characters. B) I am enjoying java. I AM ENJOYING JAVA. i am enjoying java. Character at index x = o msg has 20 characters. C) I am enjoying java. I AM ENJOYING JAVA. i am enjoying java. Character at index x = o msg has 19 characters. D) I am enjoying java. I AM ENJOYING JAVA. i am enjoying java. Character at index x = y msg has 19 characters. 46) Which of the following does not describe a valid comment in Java? A) Single line comments, two forward slashes - // B) Multi-line comments, start with /* and end with */ C) Multi-line comments, start with */ and end with /* D) Documentation comments, any comments starting with /** and ending with */ 47) Which of the following statements correctly creates a Scanner object for keyboard input? A) Scanner kbd = new Scanner(System.keyboard); B) Scanner keyboard(system.in); C) Scanner keyboard = new Scanner(System.in); D) Keyboard scanner = new Keyboard(System.in); 11

48) Which Scanner class method reads an int? A) readint() B) nextint() C) getint() D) read_int() 49) Which Scanner class method reads a String? A) readstring() B) nextstring() C) getstring() D) nextline() 50) Which one of the following methods would you use to convert a string to a double? A) Byte.ParseByte B) Long.ParseLong C) Integer.ParseInt D) Double.ParseDouble 2.2 True/False Questions 1) A Java program will not compile unless it contains the correct line numbers. 2) All Java statements end with semicolons. 3) Java is a case-insensitive language. 4) Although the dollar sign is a legal identifier character, you should not use it because it is normally used for special purposes. Answer: TRUE 5) Assuming that pay has been declared a double, the following statement is valid. pay = 2,583.44; 6) Named constants are initialized with a value, that value cannot be changed during the execution of the program. Answer: TRUE 7) A variable's scope is the part of the program that has access to the variable. Answer: TRUE 8) In Java the variable named total is the same as the variable named Total. 12

9) Class names and key words are examples of variables. 10) Both character literals and string literals can be assigned to a char variable. 11) If the compiler encounters a statement that uses a variable before the variable is declared, an error will result. Answer: TRUE 12) Programming style includes techniques for consistently putting spaces and indentation in a program so visual cues are created. Answer: TRUE 13