Computer programming 2
45 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which programming paradigm emphasizes using named memory locations for data that can be modified during program execution?

  • Procedural Programming (correct)
  • Declarative Programming
  • Object-Oriented Programming
  • Functional Programming

A software development team typically uses what programming paradigm if they intend to build a system composed of interacting, self-contained components?

  • Procedural Programming
  • Imperative Programming
  • Object-Oriented Programming (correct)
  • Functional Programming

In which programming paradigm are functions treated as 'first-class citizens,' allowing them to be assigned to variables, passed as arguments, and returned from other functions?

  • Functional Programming (correct)
  • Object-Oriented Programming
  • Logic Programming
  • Procedural Programming

Which programming paradigm is characterized by executing operations in a sequential, step-by-step manner?

<p>Procedural Programming (B)</p> Signup and view all the answers

A programmer wants to minimize the use of for, while, break, and goto statements to create code that is easier to understand and less prone to bugs. Which programming paradigm aligns best with this goal?

<p>Functional Programming (C)</p> Signup and view all the answers

When is using OOP instead of procedural programming most advantageous?

<p>When creating a model of real-world objects. (B)</p> Signup and view all the answers

Which of the following is a synonym for 'procedure' in the context of procedural programming?

<p>Function (C)</p> Signup and view all the answers

Imagine you are tasked with developing a program that models a library system, including books, patrons, and loan transactions. Which programming paradigm would be most suitable for this task?

<p>Object-Oriented Programming (D)</p> Signup and view all the answers

Which programming paradigm does OOP aim to improve upon by increasing flexibility and user-friendliness?

<p>Procedural Programming (B)</p> Signup and view all the answers

In OOP, what is the relationship between a class and an object?

<p>A class is a blueprint for an object. (D)</p> Signup and view all the answers

Which of the following is the OOP equivalent of a 'Function Call' in Procedural Programming?

<p>Message Passing (B)</p> Signup and view all the answers

Which approach does OOP utilize, emphasizing data and real-world entity modeling, in contrast to focusing primarily on procedures?

<p>Bottom-up approach (A)</p> Signup and view all the answers

What is a key advantage of OOP that involves bundling data with the methods that operate on it, thereby protecting it from unauthorized access?

<p>Data Security (D)</p> Signup and view all the answers

How does inheritance contribute to reducing the complexity of program development in OOP?

<p>By allowing classes to inherit properties and behaviors from other classes. (A)</p> Signup and view all the answers

Which of the following best describes the concept of 'reduced code duplication' in OOP?

<p>Using inheritance and polymorphism to avoid redundant code. (C)</p> Signup and view all the answers

What is the primary focus of Object-Oriented Programming (OOP) that distinguishes it from Procedural Programming?

<p>Emphasizing data and its interactions (C)</p> Signup and view all the answers

Which of the following access modifiers allows a method to be used by any other class, regardless of where it resides?

<p>public (A)</p> Signup and view all the answers

What does the static keyword signify when used in a method declaration?

<p>The method can be used without creating an instance of the class. (C)</p> Signup and view all the answers

If a method header includes void, what is the significance of this keyword?

<p>The method does not return any value to the calling method. (C)</p> Signup and view all the answers

Which of the following statements accurately describes a method's implementation?

<p>The statements within the method's body that execute the method's task. (C)</p> Signup and view all the answers

In the context of methods, what is meant by 'invoking' a method?

<p>Executing the series of statements within the method's body. (D)</p> Signup and view all the answers

If a method is described as a 'client method', what role does it play in relation to other methods?

<p>It invokes or calls another method to perform a task. (D)</p> Signup and view all the answers

A programmer wants to create a method that calculates the area of a rectangle. Which of the following method signatures is most appropriate if the method should be accessible from any class and does not require an object to be instantiated?

<p><code>public static int calculateArea (int length, int width)</code> (C)</p> Signup and view all the answers

Which of the following is NOT a valid component of a method header or declaration?

<p>Method Body (A)</p> Signup and view all the answers

What is the primary benefit of using a fully qualified identifier for a method like CompanyInfo.displayHours()?

<p>It allows the compiler to differentiate between methods with the same name in different classes. (B)</p> Signup and view all the answers

In the CompanyInfo class example, what advantage does extracting the println() statements into a separate displayHours() method provide?

<p>It makes the <code>main()</code> method shorter, easier to follow, and provides a clear intent for the separate <code>println()</code> statements. (D)</p> Signup and view all the answers

Which of the following is NOT a listed advantage of creating separate methods in Java?

<p>It allows for automatic memory management of variables within the method. (B)</p> Signup and view all the answers

What is the purpose of the class header public class Employee?

<p>It defines a new class named 'Employee' with public accessibility. (D)</p> Signup and view all the answers

Where are data fields declared within a Java class?

<p>Within a class but outside of any method. (D)</p> Signup and view all the answers

What is the significance of the curly braces {} in a class definition?

<p>They define the scope of the class body, containing data and methods. (C)</p> Signup and view all the answers

Consider a program that requires the same set of println() statements to be executed in multiple different applications. Based on the information provided, what is the best approach to achieve this?

<p>Create a separate method containing the println() statements and call that method from each application. (B)</p> Signup and view all the answers

What is the primary purpose of the DeclareTwoEmployees class?

<p>To demonstrate how to create and manipulate multiple instances of the <code>Employee</code> class. (D)</p> Signup and view all the answers

In OOP, what is the dual role that a Java class can fulfill?

<p>It can run as an application and also instantiate objects from the same class. (C)</p> Signup and view all the answers

If the Employee class does not explicitly define a constructor, what type of constructor is implicitly provided by Java?

<p>A default constructor with no parameters. (D)</p> Signup and view all the answers

What would happen if you tried to access empNum directly from the DeclareTwoEmployees class instead of using the getEmpNum() method?

<p>It would result in a compile-time error if <code>empNum</code> is declared as private in the <code>Employee</code> class. (B)</p> Signup and view all the answers

Suppose you want to extend the Employee class to create a Manager class with additional attributes. Which principle of OOP would you be applying?

<p>Inheritance (C)</p> Signup and view all the answers

If the setEmpNum method included a validation check to ensure the employee number is within a specific range (e.g., 100 to 1000), which principle of OOP would this demonstrate?

<p>Encapsulation (A)</p> Signup and view all the answers

What is the primary purpose of declaring a data field as private in a class?

<p>To control access to the field and protect its data integrity through methods. (D)</p> Signup and view all the answers

Which of the following statements best describes the difference between a static and a non-static data field?

<p>A <code>static</code> field is shared among all objects of the class, while a non-<code>static</code> field is unique to each object. (C)</p> Signup and view all the answers

In object-oriented programming, what is the common purpose of accessor methods (getters)?

<p>To retrieve the values of private data fields. (A)</p> Signup and view all the answers

What is the return type of a mutator method (setter) that is designed to modify a class field?

<p><code>void</code>, as it does not return a value. (D)</p> Signup and view all the answers

Why is it important to use accessor and mutator methods (getters and setters) instead of directly accessing data fields?

<p>It allows for better data encapsulation and control over how fields are accessed and modified. (A)</p> Signup and view all the answers

What is the first step when declaring an object as an instance of a class?

<p>Supplying a type and an identifier for the object. (D)</p> Signup and view all the answers

Consider the following code snippet: public class Example { private int value; public void setValue(int newValue) { value = newValue; } public int getValue() { return value; } } How does this code snippet demonstrate information hiding?

<p>By making the <code>value</code> field <code>private</code> and providing controlled access through <code>setValue</code> and <code>getValue</code> methods. (D)</p> Signup and view all the answers

What would be the most likely outcome if the empNum field in the Employee class was declared as public instead of private?

<p>Other classes could directly access and modify <code>empNum</code>, potentially leading to uncontrolled changes. (C)</p> Signup and view all the answers

Flashcards

Programming Paradigm

An approach to structuring and organizing code with different patterns.

Procedural Programming

A programming style where operations are executed sequentially.

Variables

Named computer memory locations that hold data.

Procedures

Blocks of code based on individual operations.

Signup and view all the flashcards

Functional Programming

A programming style focused on declarations and expressions, avoiding flow-control statements.

Signup and view all the flashcards

Arguments

Values that a function uses to perform its tasks.

Signup and view all the flashcards

Object-Oriented Programming (OOP)

An extension of procedural programming centered around objects.

Signup and view all the flashcards

Objects

Representations of real-world entities, the building blocks of OOP.

Signup and view all the flashcards

Classes

Blueprints for creating objects, defining their attributes and behaviors.

Signup and view all the flashcards

Instance Variables

Variables that hold the data of an object.

Signup and view all the flashcards

Methods

Functions associated with an object, defining its behaviors.

Signup and view all the flashcards

Message Passing

The process of one object telling another to execute a method.

Signup and view all the flashcards

OOP vs. Procedural Programming (Emphasis)

OOP emphasizes the data, while PP emphasizes procedures.

Signup and view all the flashcards

Modularity in OOP

Grouping code into classes and objects to reduce redundancy.

Signup and view all the flashcards

main() Method

The method that executes automatically when a program is run.

Signup and view all the flashcards

Call or Invoke

To execute a method.

Signup and view all the flashcards

Calling Method/Client Method

The method that calls or uses another method.

Signup and view all the flashcards

Called Method

The method that is being called by another method.

Signup and view all the flashcards

Method Header/Declaration

Provides information about how other methods can interact with a method.

Signup and view all the flashcards

Public (Access Modifier)

A keyword that allows any class to use the method.

Signup and view all the flashcards

Static (Keyword)

A keyword when any method can be used, without needing to create an object.

Signup and view all the flashcards

Non-static data field

A data field that occurs once for each object of the class.

Signup and view all the flashcards

Static data field

A data field that occurs once per class, shared by all objects.

Signup and view all the flashcards

Private access specifier

Access level that restricts access to only within the same class.

Signup and view all the flashcards

Information hiding

Hiding internal data to protect integrity, achieved through access specifiers.

Signup and view all the flashcards

Accessor methods (Getters)

Methods that retrieve the values of fields.

Signup and view all the flashcards

Mutator methods (Setters)

Methods that set (change) the values of fields.

Signup and view all the flashcards

getEmpNum()

A method used to retrieve the value of a specific field.

Signup and view all the flashcards

Object declaration

A two-step process: declare the object, then allocate the object's memory.

Signup and view all the flashcards

Setter Method

A method that sets (assigns a value to) an object's attribute or property.

Signup and view all the flashcards

Getter Method

A method that retrieves (returns) the value of an object's attribute or property.

Signup and view all the flashcards

Instantiation

Creating a new object from a class definition.

Signup and view all the flashcards

Fully Qualified Identifier

A complete name that includes the class name, a dot, and the method name.

Signup and view all the flashcards

Advantages of Separate Methods

Makes main() shorter, shows intent, methods are reusable.

Signup and view all the flashcards

Java Class Types

Classes that run programs vs. classes from which objects are created.

Signup and view all the flashcards

Class Header Parts

Optional access specifier, the keyword 'class', and a legal identifier (name).

Signup and view all the flashcards

Class Body

Holds the data and methods for the class.

Signup and view all the flashcards

Data Fields

Variables declared within a class but outside any method.

Signup and view all the flashcards

What is a Class?

A blueprint for creating objects; defines characteristics and behaviors.

Signup and view all the flashcards

Class Creation Steps

Assign a name, determine data, select methods.

Signup and view all the flashcards

Study Notes

  • Object-oriented Programming (OOP) is one of three programming paradigms, along with Procedural and Functional Programming.
  • A programming paradigm refers to different approaches to structuring and organizing code, with "paradigm" synonymous with "pattern".

Procedural Programming

  • Operations are executed sequentially, defining and using variables that refer to named computer memory locations.
  • Data can be read from input, stored, used in decisions or arithmetic, or sent to output, and can change during program execution.
  • The basic construct is blocks of code called "procedures," which are grouped logical units based on individual operations.
  • Procedures are also known as functions, modules, subroutines, and methods.
  • Procedural programs can pause to call a procedure, temporarily suspending the current logic.
  • BASIC, C, C++, and Pascal support this paradigm.

Functional Programming

  • Computer programs are built by focusing on declarations and expressions rather than the execution of statements.
  • Functional programming aims for clearer, bug-resistant code by avoiding flow-control statements like for, while, break, continue, and goto.
  • Functions are the basic units and are treated as first-class citizens: they can be assigned to a variable, passed as an argument, or returned from a function.
  • Erlang, Scala, Haskel, and Elm support this paradigm.

Object-Oriented Programming (OOP)

  • It extends procedural programming with a different approach to computer programs, using objects to model real-world objects.
  • OOP was developed to overcome the limitations of other paradigms, aiming for more flexible, user-friendly programming.
  • Writing OOP involves creating classes (blueprints for objects), creating objects (specific instances of classes), and creating applications that manipulate these objects.
  • Java, Python, VB.NET, and C# support OOP.

Naming Conventions

  • Procedural Programming relates to:
    • Variables as Objects
    • User-defined Data Types as Classes
    • Structured Members as Instance Variables
    • Functions as Methods
    • Function Call as Message Passing

Procedural vs. Object-Oriented Programming

  • Procedural Programming:
    • Emphasizes procedures over data.
    • Data isn't secured.
    • Uses a top-down approach.
    • It does not model real-world entities
    • Decomposes programs into functions or procedures
  • Object-oriented Programming:
    • Emphasizes data over procedures.
    • Data is secured.
    • Uses a bottom-up approach.
    • Models real-world entities.
    • Decomposes programs into objects

Advantages of OOP

  • Programs are modularized using classes and objects.
  • Code duplication is reduced, and reusability is increased through linking code and objects.
  • Data security is enhanced through encapsulation, making data inaccessible and unmodifiable to non-member functions.
  • Program complexity is reduced through inheritance.
  • Creation and implementation of OOP code is faster.

OOP Concepts

Class

  • A group or collection of objects with common properties and a user-defined data type for storing complex data.
  • Analogous to a blueprint before houses are built or a recipe before bread is baked.
  • A class definition describes the attributes and behaviors of its objects; attributes define an object's characteristics and serve as properties.

Object

  • A specific and concrete instance of a class; an identifiable entity with characteristics and behavior, like a person, place, or table of data.
  • Real-world entities possess data members (features) and function members (operations).

Method

  • Classes define the methods their objects can use, similar to procedures in Procedural Programming.

Examples

  • Class: Automobile
    • Object: Make, Model, Year, Color
    • Method: Forward, Backward, Gas Status
  • Class: Dog
    • Object: Breed, Name, Age, Vaccine
    • Method: Walking, Eating, Name Recognition

Data Abstraction

  • Including only essential details of an entity, excluding background details.
  • For example, focusing on using a smartphone's Bluetooth and camera features without concerning how they work internally.
  • The hidden information is called abstracted data.

Data Encapsulation

  • Wrapping up data and functions into a class; performs data hiding, which insulates data from outside programs.
  • Data is not directly accessible from outside, except through functions defined inside the class.

Inheritance

  • Creating classes that share attributes and methods of existing classes but with more specific features.
  • The class that inherits properties is the base class, parent class, or superclass.
  • The class that inherits from another is the derived class, child class, or subclass.

Example of Inheritance

  • Employees are the base class, while Manager, Supervisor, and Clerk are derived classes.
  • The employee base class inherits properties from the derived classes, with each derived class having its own properties.

Polymorphism

  • Describes the feature of languages that allows the same word or symbol to be interpreted correctly in different situations based on context.
  • Includes operator overloading and function overloading.

Operator Overloading

  • Making an operator perform tasks in different instances.
  • I.e., the addition (+) operator can perform arithmetic addition or string concatenation.

Function Overloading

  • Having two or more functions with the same name but different return types or numbers of arguments.

Methods

  • A program module containing a series of statements that carry out a task that can be invoked multiple times.
  • To execute, a programmer calls or invokes the method.
  • In other words, a calling method, or client method, invokes a called method.
  • The main() method, such as public static void main(String[] args), provides information about how other methods can interact with it.

Method Header Components

  • public - An access modifier that allows any other class to use it and not just in the class in which the method resides. Other access modifiers include private, protected, and package if left unspecified.
  • static – Used when any method can be used without instantiating an object or not requiring an object when they are called.
  • void - A return type used when a method returns no data. A return type describes the data type the method sends back to its calling method.
  • Method Name - Must be one word with no spaces, cannot be a Java keyword, and usually contains a verb.
  • Parentheses – Contains data to be sent to the method. When a main() method is written in a class, the parentheses in its header surround String[] args.
  • Method Body - The statements that carry out the work of the method. It is found between a pair of curly braces and called its implementation.

Method Placement and Execution

  • Methods must be written in the program but outside of other methods, they can never overlap.
  • The order in which methods appear does not matter, except that the main() method is always executed first.
  • A fully qualified identifier includes the class name, a dot, and the method name (e.g., CompanyInfo.displayHours()), and is necessary if the same method is used in another class.

Advantages of Creating a Separate Method

  • Using a method call to execute different println() statements makes the main() method short and easy to follow.
  • Using a well-named method makes it easy to see the overall intent of the separate println() statements.
  • AS methods are easily reusable, they can be used in any application that needs that specific action in the program.

Classes and Objects

  • Java classes can be classified into the following types:
    • Classes from which objects are not instantiated, such as the programs with the main() methods
    • Classes from which objects are created
  • With OOP, a class can be created to run as an application to instantiate objects from them and do both.
  • When creating a class, a name must be assigned to it, and the data and methods that will be part of the class must be determined.
  • A class header has three (3) parts: an optional access specifier, the keyword class, and any legal identifier in naming the class, starting with an uppercase letter (e.g., public class Employee).
  • Data components of a class are called data fields and are declared variables within a class but OUTSIDE any method. A data field is static if it occurs once per class and non-static if it happens once per object.
  • Private access specifier provides the highest security level, meaning no other classes can access the field's values except the methods of the same class.
  • The principle used in creating private access is referred to as information hiding, an important component of object-oriented programs.

Methods

  • Classes that instantiate objects contain both fields and methods.
  • Accessor methods or getters retrieve values and conventionally start with the prefix get.
  • Mutator methods or setters set or change field values and conventionally start with the prefix set.

Declaring Objects

  • Object declaration involves two steps: supplying a type and identifier, and allocating computer memory.
  • The new operator allocates memory for an object.
  • A constructor is a special type of method that creates and initializes objects.
  • Java writes constructors even when a user does not.
  • After instantiation, methods are accessed using the object's identifier, a dot, and a method call.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser