Practice Questions for Chapter 9

Similar documents
Questions Answer Key Questions Answer Key Questions Answer Key

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

Questions Answer Key Questions Answer Key Questions Answer Key

Questions Answer Key Questions Answer Key Questions Answer Key

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

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

University of Palestine. Mid Exam Total Grade: 100

Computer Programming, I. Laboratory Manual. Final Exam Solution

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Chapter 1: Introduction to Computers, Programs, and Java

Practice for Chapter 11

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

2 What are the differences between constructors and methods?

CLASS DESIGN. Objectives MODULE 4

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

Class, Variable, Constructor, Object, Method Questions

Programming - 2. Common Errors

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Instruction to students:

CS360 Lecture 5 Object-Oriented Concepts

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

Software 1 with Java. Initialization. Initialization. Initialization. Pass by Value. Initialization. Recitation No. 11 (Summary)

CS 113 MIDTERM EXAM 2 SPRING 2013

Name: Code: Lab: (circle one) 12MW 2MW 4MW 5:30MW 7:00MW

CS101 Part 2: Practice Questions Algorithms on Arrays, Classes and Objects, String Class, Stack Class

Access and Non access Modifiers in Core Java Core Java Tutorial

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Introduction to Programming Using Java (98-388)

index.pdf January 21,

COE318 Lecture Notes Week 4 (Sept 26, 2011)

Oracle 1Z Java SE 8 Programmer I. Download Full Version :

Selected Questions from by Nageshwara Rao

ITI Introduction to Computing II

ITI Introduction to Computing II

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

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Midterm Exam CS 251, Intermediate Programming March 12, 2014

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

Lecture 10 Declarations and Scope

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Full file at Chapter 2 - Inheritance and Exception Handling

WES-CS GROUP MEETING #9

ECE 122. Engineering Problem Solving with Java

CSC 1351 The Twelve Hour Exam From Hell

Introduction to Java

JAVA MOCK TEST JAVA MOCK TEST III

OO Programming Concepts

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

JAVA Programming Language Homework I - OO concept

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Programming overview

A Foundation for Programming

Chapter 8 Objects and Classes

CSE 11 Final Fall 2009

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Chapter 4 Defining Classes I

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

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Constructors for classes

Objectives. Introduce static keyword examine syntax describe common uses

Instance Members and Static Members

1B1b. Classes in Java Part III. Review. Static. Static (2) Example. Static (3) 1B1b Lecture Slides. Copyright 2004, Graham Roberts 1

Computer Science is...

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

Programming Languages and Techniques (CIS120)

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Points Missed on Page page 1 of 8

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Computational Expression

CS180 Recitation. More about Objects and Methods

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

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

1. An object of a class can be treated as an object of its corresponding class.

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Inheritance (Part 5) Odds and ends

(CONDITIONALS_BOUNDARY)

Conversions and Overloading : Overloading

ECOM 2324 COMPUTER PROGRAMMING II

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

CS111: PROGRAMMING LANGUAGE II

Lara Technologies Special-Six Test

CMSC 132: Object-Oriented Programming II

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

C++ 8. Constructors and Destructors

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

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

Exception-Handling Overview

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

Inheritance and Polymorphism

Lecture 5: Methods CS2301

COMPUTER APPLICATIONS

Transcription:

Practice Questions for Chapter 9 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) An object is an instance of a. 1) A) program B) method C) class D) data 2) is invoked to create an object. 2) A) A constructor B) A method with the void return type C) A method with a return type D) The main method 3) Which of the following statements are true? 3) A) Constructors must have the same name as the class itself. B) Multiple constructors can be defined in a class. C) Constructors are invoked using the new operator when an object is created. D) Constructors do not have a return type, not even void. 4) What is wrong in the following code? 4) class TempClass { int i; public void TempClass(int j) { int i = j; public class C { TempClass temp = new TempClass(2); A) The program has a compilation error because TempClass does not have a constructor with an int argument. B) The program compiles and runs fine. C) The program compiles fine, but it does not run because class C is not public. D) The program has a compilation error because TempClass does not have a default constructor. 5) Variables that are shared by every instances of a class are. 5) A) public variables B) instance variables C) private variables D) class variables 1

6) Analyze the following code. 6) int x; public Test(String t) { System.out.println("Test"); Test test = new Test(); System.out.println(test.x); A) The program has a compile error because Test does not have a default constructor. B) The program has a compile error because x has not been initialized. C) The program has a compile error because you cannot create an object from the class that defines the object. D) The program has a compile error because System.out.println method cannot be invoked from the constructor. 7) What is the printout of the third println statement in the main method? 7) int i; static int s; Foo f1 = new Foo(); System.out.println("f1.i is " + f1.i + " f1.s is " + f1.s); Foo f2 = new Foo(); System.out.println("f2.i is " + f2.i + " f2.s is " + f2.s); Foo f3 = new Foo(); System.out.println("f3.i is " + f3.i + " f3.s is " + f3.s); public Foo() { i++; s++; A) f3.i is 3 f3.s is 1 B) f3.i is 1 f3.s is 3 C) f3.i is 1 f3.s is 2 D) f3.i is 3 f3.s is 3 E) f3.i is 1 f3.s is 1 8) A method that is associated with an individual object is called. 8) A) an object method B) an instance method C) a static method D) a class method 2

9) Analyze the following code. 9) int x; public Test(String t) { System.out.println("Test"); Test test = null; System.out.println(test.x); A) The program has a compile error because you cannot create an object from the class that defines the object. B) The program has a compile error because test is not initialized. C) The program has a runtime NullPointerException because test is null while executing test.x. D) The program has a compile error because x has not been initialized. E) The program has a compile error because Test does not have a default constructor. 10) Suppose the xmethod() is invoked from a main method in a class as follows, xmethod() is in the class. 10) xmethod(); A) a static method or an instance method B) a static method C) an instance method 11) Analyze the following code and choose the best answer: 11) private int x; Foo foo = new Foo(); System.out.println(foo.x); A) 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. B) You cannot create a self-referenced object; that is, foo is created inside the class Foo. C) Since x is private, it cannot be accessed from an object foo. D) 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. 3

12) What is the printout for the first statement in the main method? 12) static int i = 0; static int j = 0; int i = 2; int k = 3; { int j = 3; System.out.println("i + j is " + i + j); k = i + j; System.out.println("k is " + k); System.out.println("j is " + j); A) i + j is 22 B) i + j is 23 C) i + j is 5 D) i + j is 6 13) Which of the following are properties of a constructor? 13) A) A constructor is called using the new operator. B) A constructor must have the same name as the class. C) Constructors may be overloaded. 14) Analyze the following code: 14) class Test { private double i; public Test(double i) { this.t(); this.i = i; public Test() { System.out.println("Default constructor"); this(1); public void t() { System.out.println("Invoking t"); A) this.i may be replaced by i. B) this(1) must be called before System.out.println("Default constructor"). C) this(1) must be replaced by this(1.0). D) this.t() may be replaced by t(). 4

15) Java assigns a default value to a data member of a class if the data is not initialized. 15) A) true B) false 16) Given the declaration Circle[] x = new Circle[10], which of the following statement is most accurate? A) x contains a reference to an array and each element in the array can hold a reference to a Circle object. B) x contains an array of ten objects of the Circle type. C) x contains an array of ten int values. D) x contains a reference to an array and each element in the array can hold a Circle object. 17) What is the printout for the first statement in the main method? 16) 17) private int i = 0; static int j = 0; new Test(); public Test() { i++; j++; int i = 1; int j = 1; System.out.println("i is " + j + " j is " + j); A) i is 1 j is 0 B) i is 1 j is 1 C) i is 0 j is 0 D) i is 2 j is 2 E) i is 0 j is 1 5