Programming Languages Exam 3

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall with Kai
GameKnowt Play
New
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/82

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

83 Terms

1
New cards

abstraction

the view or representation of an entity that includes only the most significant attributes; allows one to collect instances of entities into groups in which their common attributes need not be considered

- simplifies the programming process

2
New cards

What are the two fundamental kinds of abstraction?

process abstraction and data abstraction

3
New cards

process abstraction

among the oldest in programming language design; all subprograms are these because they provide a way for a program to specify a process, without providing details of how it performs its task (at least in the calling program)

4
New cards

What is an essential part of data abstraction?

Its operations, which are defined as process abstractions.

5
New cards

abstract data type

data structure, in the form of a record, but which includes subprograms that manipulate its data; enclosure that includes only the data representation of one specific data type and the subprograms that provide the operations for that type

6
New cards

An instance of an abstract data type is called a/an

object

7
New cards

T/F: all built-in types are abstract data types

T

8
New cards

T/F: information hiding is a key concept in data abstraction

T; provides portability

9
New cards

What characteristics do user-defined abstract data types contain?

1. a type definition that allows program units to declare variables of the type but hides the representation of objects of the type

2. a set of operations for manipulating objects of the type

10
New cards

benefits of information hiding

- increased reliability

- reduces the range of code and number of variables of which a programmer must be aware when writing or reading a part of the program

- makes name conflicts less likely, because the scopes of variables is smaller

- increased flexibility

11
New cards

clients

program units that use a specific abstract data type

12
New cards

What are the reasons accessors are better than making data public?

1. read-only access can be provided by having a getter method but no corresponding setter method

2. constraints can be included in setters; for example, if the data value should be restricted to a particular range, the setter can enforce that

3. the actual implementation of the data member can be changed without affecting the clients if getters and setters are the only access

13
New cards

What operations are required by many abstract data types?

- iterators

- accessors

- constructors

- destructors (often used to reclaim heap storage that may be used by parts of abstract data type objects in languages that do not do implicit storage reclamation)

14
New cards

What are some examples of languages that directly support abstract data types?

C++, Java, C#, and Ruby

15
New cards

design issues of ADTs

- whether abstract data types can be parameterized

- what access controls are provided and how such controls are specified

- language designer must decide whether the specification of the type is physically separate from its implementation (or whether that is a developer choice)

16
New cards

data defined in a C++ class

data members

17
New cards

the functions (methods) defined in a class

member functions

18
New cards

implicitly inlined

when a member function has its header and body in the class definition, rather than separating them

19
New cards

T/F: placing member function definitions outside the class definition separates specification from implementation, a common goal of modern programming

T

20
New cards

protected members in C++

makes a member visible to subclasses, but not to clients

21
New cards

T/F: destructors are implicitly called in C++

T (ex. of destructor with class name Cat: ~Cat( ))

22
New cards

Java ADTs

- all objects are allocated from the heap and accessed through reference variables

- methods must be defined completely in a class

- a method body must appear with its corresponding method header (both declared and defined in a single syntactic unit)

- access modifiers are attached to each method or variable definition rather than having them in groups

23
New cards

advantage of Java ADTs over C++ ADTs

It uses implicit garbage collection of all objects.

- it allows the programmer to ignore manual deallocation

24
New cards

Java print statement syntax

System.out.println(" ");

25
New cards

C# ADTs

- combination of both C++ and Java, with new constructs

- like Java, all class instances are heap dynamic

- default constructors are predefined

- allows destructors to be defined, but it uses garbage collection for most of its heap objects, so rarely used

- uses same access modifier setup as Java

- implicitly uses getters and setters (inherited from Delphi)

26
New cards

C# structs

- lightweight classes

- like classes, but do not support inheritance

- value types, as opposed to reference types

- allocated on run-time stack instead of the heap

- all C# value types, including its primitive types, are these

27
New cards

Ruby ADTs

- class methods are distinguished from instance methods by having the class name appended to the beginning of their names with a period separator

- constructors are named "initialize" and cannot be overloaded, so only one per class

- dynamic, members can be added at any time

- methods can be removed from a class with "remove_method"

28
New cards

T/F: Adding flexibility for dynamic classes in Ruby harms readability.

T; to determine the behavior of a class at a particular point in a program, one must find all of its definitions in the program and consider all of them

29
New cards

Ruby access control

method access control is dynamic, so access violations are detected only during execution

- default method access is public, but can also be protected or private

- can specify access control before function or at end of class without parameters

(ex. def method7

end

private: method7

)

30
New cards

Ruby attributes

instance data that is accessible through accessor methods

31
New cards

Ruby setters syntax

def sum=(new_sum)

@sum = new_sum

end

getters:

def sum

@sum

end

32
New cards

How can getters and setters in Ruby be implicitly generated?

include calls to attr_reader (getter) and attr_writer (setter)

ex. attr_reader :sum, :total

attr_writer :sum

33
New cards

T/F: Arrays in Ruby are not arrays of references to objects.

F; are arrays of references to objects

34
New cards

What is the difference between how static typed and dynamic typed languages store type elements?

Static typed languages require specific code changes (templates in C++) to accept any type as class parameters, while dynamic typed (like Ruby) do not require any changes since dynamic.

35
New cards

What were the problems with original collection types in Java?

1. every time an object was removed from the collection, it had to be cast to the appropriate type

2. no error checking when elements were added to the collection

3. the collection types could not store primitive types

- instead, uses new when instantiating classes to specify type, and uses "extends" ClassName when dependent on a class

36
New cards

C# parameterized ADTs (defining generic type)

- first used collection classes like Java, but ran into same issues

- now uses generic classes (predefined ones are Array, List, Stack, Queue, and Dictionary)

- eliminates problems of allowing mixed types in collections and requiring casts when objects are removed from the collections

- user can define generic classes; can be defined for its elements to be indexed

37
New cards

What is one capability that Java provides that C# does not provide with parameterized ADTs?

wildcard classes

38
New cards

encapsulation

organized programs into collections of logically related code and data, each of which can be compiled without recompilation of the rest of the program

- often placed in libraries and made available for reuse in programs other than those for which they were written

39
New cards

examples of encapsulation

- C++ and C using header and implementation files

- C# using the .NET programming languages utility: the assembly (built by .NET compilers, where a .NET application consists of one or more assemblies)

40
New cards

assembly (in terms of .NET usage)

a file that appears to application programs to be a single dynamic link library or an executable

- internal access modifier in C# is visible to all classes in the assembly in which it appears

41
New cards

naming encapsulations

- C++ and C# namespace keyword

- Java package keyword, import keyword from package (no friend functions/classes)

- Ruby module keyword, require 'fileName'

42
New cards

Which language was the first to offer complete support for object-oriented programming?

Smalltalk

43
New cards

What three key language features must OOL provide?

- ADTs (encapsulation)

- inheritance

- dynamic binding of method calls to methods (polymorphism)

44
New cards

What are classes called that are defined through inheritance?

child class/derived class/subclass, where main class is called parent/base/superclass class

45
New cards

In inheritance, a new method is said to _________ the inherited method.

override; which is then called an overridden method

46
New cards

multiple inheritance

processes where classes have more than one parent class

47
New cards

abstract method

where a protocol is defined in a base class, but never called; if a base class contains at least one of these, the class is called an abstract class; usually cannot be instantiated, only its subclasses with full implementation can be instantiated; purely virtual function

48
New cards

principle of substitution

if a language allows programs in which a variable of a class can be subed for a variable of one of its ancestor classes in any situation, without causing type errors and without changing the behavior of the program, the language supports this principle

49
New cards

What is used as an alternative to multiple inheritance to avoid issues of complexity and efficiency?

interfaces, where methods are declared but not defined

50
New cards

object slicing

when excess space not able to fit on the stack is truncated

51
New cards

Ruby syntax for defining a child class

class MyChildClass < BaseClass

52
New cards

T/F: Ruby is compiled.

F; Ruby is interpreted

53
New cards

class instance record (CIR)

structure for instance variables of a C++ class; Object-level structure storing instance data and vtable pointer.

54
New cards

virtual method table (vtable)

Class-level structure storing function pointers for dynamic dispatch.

55
New cards

virtual method

a method that is allowed to be overridden

56
New cards

reflection

provides the possibility of late binding of calls to methods that are outside the inheritance hierarchy of the calling code; allows run-time access to dynamically modify behavior

57
New cards

metadata

types and structure of a program

58
New cards

introspection

process of a program examining its metadata

59
New cards

ways program can modify behavior dynamically

- could use metadata

- could change metadata directly

- could intercede in the execution of the program

60
New cards

reflection in different languages

- Java (uses Class objects and .getClass() methods

- C# uses Type, .GetType, MethodInfo .GetMethod

61
New cards

problems with using reflection

- performance suffers

- exposes private fields and methods

- operations may not work when code is run under a security manager, also making it non-portable

62
New cards

irb

interactive Ruby

63
New cards

str.sub vs str.gsub

sub only replaces first occurrence, gsub replaces all

64
New cards

str.sub(/^../,"House")

replaces first (^) two letters (..) of string with House

65
New cards

str.sub(/..$/,"House")

replaces last two letters of string with House

66
New cards

str.scan(/./)

scans for every single character; (.) indicates every character besides newline, even spaces

67
New cards

str.scan(/\w\w/) { |x| puts x }

puts consecutive word characters beside each other, each on a separate line (excluding spaces since not word characters)

68
New cards

str.scan(/\W/) { |x| puts x }

matches any non-word character

69
New cards

s.scan(/\d/) { |x| puts x}

puts all digits in a string on a separate line

70
New cards

s.scan(/\d\d/) { |x| puts x}

puts consecutive digits on a separate line (will omit digits that aren't consecutive)

71
New cards

s.scan(/\D/) { |x| puts x}

includes everything other than a digit on a separate line

72
New cards

s.scan(/\d{2}/) { |x| puts x}

prints exactly 2 consecutive digits

73
New cards

s.scan(/\d{2,3}/) { |x| puts x}

2 or 3 consecutive digits

74
New cards

s.scan(/d+/)

puts all d occurrences in an array; if none, just puts "d" in an array

75
New cards

s.scan(/\d*/) { |x| puts x }

scans for 0 or more digits, including space characters

76
New cards

s.scan(/\s/) { |x| puts x }

whitespace

77
New cards

s.scan(/[abcABC]/) { |x| puts x }

scans for all included characters

78
New cards

puts "Average is #{"%.1f"%average}"

print statement when including rounding

79
New cards

CSV.open('students2.csv','w') do |csv|

10 students.each do |stdnt|

11 csv << stdnt

12 end

13 end

writing to a CSV file

80
New cards

a = Square.new(3)

puts a.perimeter

use Class.new(parameters) when creating a class object

81
New cards

def self.count

@@numSquares

end

a = Square.new

Square.count

referring to class itself, not particular instance

82
New cards

system() vs exec()

system does operation then continues in code, exec does operation and then terminates program

83
New cards

#!/usr/local/bin/ruby

2

3 f = fork # same usage as Perl fork

4

5 if f.nil? # if 0

6 puts "Child"

7 else

8 puts "Parent"

9 end

IPC with pipes in Ruby