Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

Similar documents
Java Simple Data Types

Java Simple Data Types

Key Java Simple Data Types

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Classes. Classes as Code Libraries. Classes as Data Structures

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Recitation 3 Class and Objects

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

Notes on Chapter Three

JAVA Programming Language Homework I - OO concept

CS 302 Week 9. Jim Williams

Ch 7 Designing Java Classes & Class structure

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

1.00 Lecture 8. Using An Existing Class, cont.

Answer ALL Questions. Each Question carries ONE Mark.

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

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

Software and Programming 1

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Encapsulation in Java

(a) Write the signature (visibility, name, parameters, types) of the method(s) required

Getting started with Java

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

Object-Oriented Programming (Java)

Principles of Object Oriented Programming. Lecture 4

Methods and Data (Savitch, Chapter 5)

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

Preview from Notesale.co.uk Page 3 of 36

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Abstract Classes Interfaces CSCI 201 Principles of Software Development

Java Object Oriented Design. CSC207 Fall 2014

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Introduction to Computer Science I

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Classes and Methods: Classes

Introduction to Java. Handout-1d. cs402 - Spring

Object Oriented Programming in C#

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Arrays Classes & Methods, Inheritance

Module - 3 Classes, Inheritance, Exceptions, Packages and Interfaces. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS 113 MIDTERM EXAM 2 SPRING 2013

Introduction to Programming Using Java (98-388)

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Arrays. CSE 142, Summer 2002 Computer Programming 1.

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

Programming II (CS300)

coe318 Lab 1 Introduction to Netbeans and Java

Lecture 02, Fall 2018 Friday September 7


*An nested class is a class that is defined inside another class.

Programming II (CS300)

PROGRAMMING FUNDAMENTALS

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

Overview of Eclipse Lectures. Module Road Map

(5 2) Introduction to Classes in C++ Instructor - Andrew S. O Fallon CptS 122 (February 7, 2018) Washington State University

Recommended Group Brainstorm (NO computers during this time)

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Object-Oriented Programming

Object Oriented Programming (II)

Programming Language Concepts: Lecture 1

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Introduction to Object-Oriented Programming

CS1004: Intro to CS in Java, Spring 2005

This Week. Fields and Variables. W05 Example 1: Variables & Fields. More on Java classes. Constructors. Modifiers

Programming by Delegation

CS 113 PRACTICE FINAL

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Lec 7. for loops and methods

Chapter 4: Writing Classes

1.00 Introduction to Computers and Engineering Problem Solving Quiz 1 March 4, 2005

Interfaces & Generics

Anatomy of a Class Encapsulation Anatomy of a Method

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Lab5. Wooseok Kim

Lecture 5: Methods CS2301

Chapter 6 Lab Classes and Objects

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

COMP200 - Object Oriented Programming: Test One Duration - 60 minutes

CS 1331 Exam 1 ANSWER KEY

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

Chapter 2: Java OOP I

Selected Questions from by Nageshwara Rao

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Nested Loops. A loop can be nested inside another loop.

Objects as a programming concept

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

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Getter and Setter Methods

CMPS 134: Computer Science I Fall 2011 Test #1 October 5 Name Dr. McCloskey

55:182/22C:182. Distributed Application Frameworks Java RMI, CORBA, Web Services (SOAP)

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS 201, Fall 2016 Sep 28th Exam 1

Questions Answer Key Questions Answer Key Questions Answer Key

Transcription:

Peer Instruction 8 Classes and Objects How can multiple methods within a Java class read and write the same variable? A. Allow one method to reference a local variable of the other B. Declare a variable of the same name in both methods C. Add the variable to the class as a class variable D. Pass the variable as a parameter between methods E. None of the above Question - 1 How can multiple methods within a Java class read and write the same variable? A. Allow one method to reference a local variable of the other B. Declare a variable of the same name in both methods C. Add the variable to the class as a class variable D. Pass the variable as a parameter between methods E. None of the above Question - 2 1

objects and classes are correct? 1) In Java, code and data can only exist in a class. 2) Instantiation does not require memory allocation. 3) Instantiation makes a class from an object. 4) Many objects can be made from a single class. 5) Only a single object can be made from a class. A. 1) and 3) B. 1) and 4) C. 2) and 3) D. 2) and 4) E. 1) and 5) Question - 3 objects and classes are correct? 1) In Java, code and data can only exist in a class. 2) Instantiation does not require memory allocation. 3) Instantiation makes a class from an object. 4) Many objects can be made from a single class. 5) Only a single object can be made from a class. A. 1) and 3) B. 1) and 4) C. 2) and 3) D. 2) and 4) E. 1) and 5) Question - 4 public versus private are correct? A. Public variables and methods cannot be accessed outside the class in which they are defined. B. Private variables can be accessed outside the class only by writing getter or setter methods. C. Private methods cannot be non-static, but public methods can be, and both can be static. D. Private methods comprise the interface provided to users of the class. E. If you instantiate a class from outside the class you can access both private and public variables. Question - 5 2

public versus private are correct? A. Public variables and methods cannot be accessed outside the class in which they are defined. B. Private variables can be accessed outside the class only by writing getter or setter methods. C. Private methods cannot be non-static, but public methods can be, and both can be static. D. Private methods comprise the interface provided to users of the class. E. If you instantiate a class from outside the class you can access both private and public variables. Question - 6 static and non-static are correct? A. Static data is also called instance data, and non-static data is called class data. B. Only one copy of instance (non-static) exists. C. There is a separate copy of instance data for every object that is instantiated. D. Accessing class data using the class name instead of the object name is not a good practice. E. Accessing instance data does not require use of the class name, if done from within the same class. Question - 7 static and non-static are correct? A. Static data is also called instance data, and non-static data is called class data. B. Only one copy of instance (non-static) exists. C. There is a separate copy of instance data for every object that is instantiated. D. Accessing class data using the class name instead of the object name is not a good practice. E. Accessing instance data does not require use of the class name, if done from within the same class. Question - 8 3

The code below accesses class/instance variables, which line will not compile? 0 public class Class { 1 String s0 = Instance Data ; 2 static String s1 = Class Data ; 3 public static void main(string args[]) { 4 Class instance = new Class(); 5 System.out.println(Class.s0); 6 System.out.println(instance.s0); 7 System.out.println(Class.s1); 8 System.out.println(instance.s1); A. 5 B. 6 C. 7 D. 8 E. All will compile Question - 9 The code below accesses class/instance variables, which line will not compile? 0 public class Class { 1 String s0 = Instance Data ; 2 static String s1 = Class Data ; 3 public static void main(string args[]) { 4 Class instance = new Class(); 5 System.out.println(Class.s0); 6 System.out.println(instance.s0); 7 System.out.println(Class.s1); 8 System.out.println(instance.s1); A. 5 B. 6 C. 7 D. 8 E. All will compile Question - 10 Putting it all together with class and instance data. public class Peer { A. 11 44 11 66 static int i = 11; B. 33 44 55 66 int j = 22; C. 55 44 55 66 D. 55 66 55 66 public static void main(string args[]) { E. Will not compile Peer p1 = new Peer(); Peer p2 = new Peer(); p1.i = 33; p1.j = 44; p2.i = 55; p2.j = 66; System.out.println(p1.i+ +p1.j+ +p2.i+ +p2.j); Question - 11 4

Putting it all together with class and instance data. public class Peer { A. 11 44 11 66 static int i = 11; B. 33 44 55 66 int j = 22; C. 55 44 55 66 D. 55 66 55 66 public static void main(string args[]) { E. Will not compile Peer p1 = new Peer(); Peer p2 = new Peer(); p1.i = 33; p1.j = 44; p2.i = 55; p2.j = 66; System.out.println(p1.i+ +p1.j+ +p2.i+ +p2.j); Question - 12 5