CONCEPTS OF
PROGRAMMING LANGUAGES
TENTH EDITION
Robert W. Sebesta
Chapter 12-Support for Object-Oriented Programming
Review Question
2. What are the problems associated with programming using abstract data types?
Answer :
-In nearly all cases, the features and capabilities of the existing type are not quite right for the new use.
-The type definitions are all independent and are at the same level.
4. What is message protocol?
Answer : Message protocol is the entire collection of methods of an object.
5. What is an overriding method?
Answer : Overriding method is method that overrides the inherited method.
7. What is dynamic dispatch?
Answer : Dynamic dispatch is the third characteristic (after abstract data types and inheritance) of object-oriented programming language which is a kind of polymorhphism provided by the dynamic binding of messages to method definitions.
8. What is an abstract method ? What is an abstract class ?
Answer : a method which all descendant classes should have. An abstract class is a class which implement abstract method.
12. From where are Smalltalk objects allocated?
Answer : Smalltalk objects are allocated from the heap and are referenced through reference variables, which are implicitly dereferenced.
15. What kind of inheritance, single or multiple, does Smalltalk support?
Answer : Smalltalk supports single inheritance; it does not allow multiple inheritance.
19. How are C++ heap-allocated objects deallocated?
Answer : C++ heap-allocated objects are deallocated using destructor.
25. What is mixins in objective-C?
Answer : Mixins are sometimes used to add certain functionalities to different classes. And, of course, the class still has a normal superclass from which it inherits members. So, mixins provide some of the benefits of multiple inheritance, without the naming collisions that could occur if modules did not require module names on their functions.
29. Does Objective-C support multiple inheritance?
Answer : No Objective-C doesn’t support it. (It supports only single inheritance).
33. What is the purpose of an Objective-C category?
Answer : The purpose of an Objective-C category is to add certain functionalities to different classes and also to provide some of the benefits of multiple inheritance, without the naming collisions that could occur if modules did not require module names on their functions.
38. What is boxing?
Answer : Boxing is primitive values in Java 5.0+ which is implicitly coerced when they are put in object context. This coercion converts the primitive value to an object of the wrapper class of the primitive value’s type.
39. How are Java objects deallocated?
Answer : By implicitly calling a finalizemethod when the garbage collector is about to reclaim the storage occupied by the object.
Rieview Question
1 . What important part of support for inheritance is missing in Java?
Answer : Java does not support the private and protected access control, unlike in C++ user can determine which data member can be inherit by using protected access control.
3. Compare the inheritance of C++ and Java.
Answer :
-In Java, all objects are Inherited, either directly or indirectly. While in C++ a class can be defined to stand on its own without an ancestor.
-In Java, methods are virtual by default. In C++, we explicitly use virtual keyword.
-Java uses a separate keyword interface for interfaces, and abstract keyword for abstract classes and abstract functions.
-In Java, there’s no access level specifier of inheritance, while C++ has private public and protected.
-Unlike C++, Java doesn’t support multiple inheritance. A class cannot inherit from more than one class. A class can implement multiple interfaces though.
7. What is one programming situation where multiple inheritance has a significant disadvantage over interfaces ?
Answer:
When two or more parent classes are derived from one grandparent class and they have one same child. (diamond problem)
9. Given an example of inheritance in C++, where a subclass overrides the superclass methods.
Answer:
class car{
public :
void engine()
{std::cout<<”120 hp”;}
}
class sportcar : public car{
public :
void enginge()
{std::cout<<”580 hp”;}
}
10. Explain one advantage of inheritance
Answer:
One advantage of inheritance is more easier for update of a program. Because child classes are inherited from the parent to if there are any update the programmer can only focus on the parent.
The attitude or function in the parent will automatically inherited to the children.
12. Compare inheritance and nested classes in C++. Which of these supports an is-a relationship?
Answer :
Inheritance is where one class (child class) inherits the members of another class (parent class).Nested class is a class declared entirely within the body of another class or interface.
17. What are the different options for object destruction in Java?
Answer :
Finalize is related to C++ destructor. A finalize method is implicitly called when the garbage collector is about to reclaim the storage occupied by the object. The problem with finalize is that the time it will run cannot be forced or even predicted. The alternative to using finalize to reclaim resources held by an object about to be garbage collected is to include a method that does the reclamation. The only problem with this is that all clients of the objects must be aware of this method and remember to call it.
21. Compare the support for polymorphism in C# with that of in Objective-C.
Answer :
In Objective-C, polymorphism is implemented in a way that differs from the way it is done in most other common programming languages. A polymorphic variable is created by declaring it to be of type id. Such a variable can reference any object. The run-time system keeps track of the class of the object to which
an id type variable refers. If a call to a method is made through such a variable, the call is dynamically bound to the correct method, assuming one exists.
To allow dynamic binding of method calls to methods in C#, both the base method and its corresponding methods in derived classes must be specially marked. The base class method must be marked with virtual, as in C++. To make clear the intent of a method in a subclass that has the same name and protocol as a virtual method in an ancestor class, C# requires that such methods be marked override if they are to override the parent class virtual method.
25. Study and explain private and public modifiers in C++. How do those modifiers differ in C#?
Answer :
C++ includes both classes and structs, which are nearly identical constructs. The only difference is that the default access modifier for class is private, whereas for structs it is public. C# also has structs, but they are very different from those of C++. In C#, structs are, in a sense, lightweight classes. They can have constructors, properties, methods, and data fields and can implement interfaces but do not support
Special thanks to Mr. Tri Djoko Wahjono, Ir., M.Sc.
Rabu, 26 Juni 2013
CONCEPTS OF PROGRAMMING LANGUAGES TENTH EDITION Robert W. Sebesta Chapter 11-Abstract Data Type and Encapsulation
CONCEPTS OF
PROGRAMMING LANGUAGES
TENTH EDITION
Robert W. Sebesta
Chapter 11-Abstract Data Type and Encapsulation
1. What are the two kinds of abstraction in programming languages?
Answer: The two kinds of abstraction in programming languages are process abstraction and data abstraction.
2. Define abstract data type.
Answer: Abstract data type is a data structure that include its data member and also member function that process its data.
6 . Explain how information hiding is provided in Ada package.
Answer: Data type representations can appear in the package specification but be hidden from clients by putting them in the private clause of the package. The abstract type itself is defined to be private in the public part of the package specification. Private types have built-in operations for assignment and comparison for equality and inequality.
10. What is the use of the Ada with clause ?
Answer: To make the names defined in external packages visible
15. What is the purpose of a C++ destructor?
Answer: Destructor in C++ can be used to print a value of some or all of the object's data members and it can deallocate an object from a class.
16. What are the legal return types of a destructor?
Answer: there are no legal return from a destructor and constructor. It is like void function that has no return value.
20. What is the use of limited private types?
Answer: An alternative to private types is a more restricted form: limited private types. Non-pointer limited private types are described in the private section of a package specification, as are non-pointer private types. The only syntactic difference is that limited private types are declared to be limited private in the visible part of the package specification. The semantic difference is that objects of a type that is declared limited private have no built-in operations. Such a type is useful when the usual predefined operations of assignment and comparison are not meaningful or useful. For example, assignment and comparison are rarely used for stacks.
21. What are initializes in Objective-C?
Answer: The initializes in Objective-C are constructors.
22. What is the use of @private and @public directives?
Answer: The use is to specify the access levels of the instance variables in a class definition.
27. Where are all Java methods defined?
Answer: All Java methods are defined in a class.
37. What is the name of all Ruby constructors?
Answer: Constructors in Ruby are named initialize.
43. What is a C++ namespace, what is its purpose?
Answer: In general, a namespace is a container for a set of identifiers and allows the disambiguation of homonym identifiers residing in different namespaces. The purpose is to help programs manage the problem of global namespace.
problem set:
4. What are the advantages of the non-pointer concept in Java?
Answer:
Advantages:
-It reduce the chance of memory leak
-No nameless variable
Disadvantage:
-Not allowed to declare pointer variable
-Increase the complexity of task related to data structure like array, structure ect.
9. What happens if the constructor is absent in Java and C++?
Answer:
If there is no constructor in java and C++ so the compiler will set a default constructor.
10. Which two conditions make data type “abstract”?
Answer:
• The representation of objects of the type is hidden from the program units that use the type, so the only direct operations possible on those objects are those provided in the type’s definition.
• The declarations of the type and the protocols of the operations on objects of the type, which provide the type’s interface, are contained in a single syntactic unit. The type’s interface does not depend on the representation of the objects or the implementation of the operations. Also, other program units are allowed to create variables of the defined type.
11. Why is the destructor of C# rarely used?
Answer: Although C# allows destructors to be defined, because it uses garbage collection for most of its heap objects, destructors are rarely used.
13. Compare and contrast the data abstraction of Java and C++.
Java support for abstract data types is similar to that of C++. There are, however, a few important differences. All objects are allocated from the heap and accessed through reference variables. Methods in Java must be defined completely in a class. A method body must appear with its corresponding method
header. Therefore, a Java abstract data type is both declared and defined in a single syntactic unit. A Java compiler can inline any method that is not overridden. Definitions are hidden from clients by declaring them to be private. Rather than having private and public clauses in its class definitions, in Java access modifiers can be attached to method and variable definitions. If an instance variable or method does not have an access modifier, it has package access.
Special thanks to Mr. Tri Djoko Wahjono, Ir., M.Sc.
CONCEPTS OF PROGRAMMING LANGUAGES TENTH EDITION Robert W. Sebesta Chapter 10-Implementing Subprogram
CONCEPTS OF
PROGRAMMING LANGUAGES
TENTH EDITION
Robert W. Sebesta
Chapter 10-Implementing Subprogram
Review Question
1. What is the definition used in this chapter for “simple” subprograms ?
Answer: Simple subprograms are not nested and all variables in it are static
2. Which of the caller of called saves execution status information ?
Answer: Either can save the execution status
4. What is the task of a linker?
Answer:
-find the files that contain the translated subprograms referenced in that program
-load the files that contains translated subprogram into memory.
-set the target addresses of all calls to those subprograms in the main program to the entry addresses of those subprograms.
8. What kind of machines often use registers to pass parameters ?
Answer: RISC Machines
10. Define static chain, static_depth, nesting_depth,and chain_offset.
Answer:
static chain : a chain of static links that connect certain activation record instances in the stack.
static_depth : integer associated with a static scope that indicates how deeply it is nested in the outermost scope.
nesting_depth : difference between static_depth of a subprogram containing reference to x and the static_depth of a subprogram containing the declaration of x.
chain_offset : number of links to the correct activation record instance
12. How are references to variables represented in the static-chain method ?
Answer: It is represented by static_depth.
17. Describe the shallow-access method of implementing dynamic scoping.
Answer:
Shallow access is an alternative implementation method. The semantic of shallow and deep access are identical.
In the shallow-access method, variables declared in subprograms are not stored in the activation records of those subprograms.
Problem Set
7. It is stated in this chapter that when non-local variables are accessed in a dynamic-scoped language using the dynamic chain, variable names must be stored in the activation records with the values. If this were actually done, every non-local access would require a sequence of costly string comparisons on names. Design an alternative to these string comparisons that would be faster.
Answer:
One very simple alternative is to assign integer values to all variable names used in the program. Then the integer values could be used in the activation records, and the comparisons would be between integer values, which are much faster than string comparisons.
8. Pascal allows gotos with non-local targets. How could such statements be handled if static chains were used for non-local variable access?
Answer:
Finding the correct activation record instance of a non-local variable using static links is relatively straightforward. When a reference is made to non-local variable, the activation record instance containing the variable can be found by searching the static chain until a static ancestor activation record instance is found that contains the variable.
9. The static-chain method could be expanded slightly by using two static links in each activation record instance where the second points to the static grandparent activation record instance. How would this approach affect the time required for subprogram linkage and non-local references?
Answer:
The nesting of scopes is known at compile time, the compiler can determine not only that a reference is non-local but also the length of the static chain that must be followed to reach the activation records instance that contains the non-local object.
11. If a compiler uses the static chain approach to implementing blocks, which of the entries in the activation records for subprograms are needed in the activation records for blocks?
Answer:
A static chain is a chain of static links that connect certain activation record instances in the stack. During the execution of a subprogram P, the static link of its activation record instance points to an activation record instance of P’s static parent program unit. That instance’s static link points in turn to P’s static grandparent program unit’s activation record instance, if there is one. So, the static chain connects all the static ancestors of an executing subprogram, in order of static parent first. This chain can obviously be used to implement the accesses to non-local variables in static-scoped languages.
Special thanks to Mr. Tri Djoko Wahjono, Ir., M.Sc.
CONCEPTS OF PROGRAMMING LANGUAGES TENTH EDITION Robert W. Sebesta Chapter 9-Subprogram
CONCEPTS OF
PROGRAMMING LANGUAGES
TENTH EDITION
Robert W. Sebesta
Chapter 9-Subprogram
Review Question
1. What are the three general characteristics of subprograms?
Answer: Each subprogram has a single entry point, excluding co-routine.
The calling program is suspended during the execution of the called subprogram, which implies that there is only one subprogram in execution at any given time.
Control always returns to the caller when the subprogram execution terminates.
2. What does it mean for a subprogram to be active?
Answer: It means that after having been called, a subprogram has begun execution but has not yet completed that execution.
4. What are formal parameters? What are actual parameters?
Answer: The parameters in the subprogram header are called formal parameters.
Actual parameters are some names of the subprogram and a list of parameters to be bounded to the formal parameters of the subprogram when a subprogram call a statement
5. What languages allow a variable number of parameters ?
Answer: C,C++,Perl JavaScript, and Lua
6. What is a Ruby array formal parameter?
Answer: Ruby supports a complicated but highly flexible actual parameter configuration. The initial parameters are expressions, whose value objects are passed to the corresponding formal parameters. The initial parameters can be following by a list of key => value pairs, which are placed in an anonymous hash and a reference to that hash is passed to the next formal parameter. These are used as a substitute for keyword parameters, which Ruby does not support. The hash item can be followed by a single parameter preceded by an asterisk. This parameter is called the array formal parameter.
7. What is a parameter profile? What is a subprogram protocol?
Answer: Parameter profile is the number, order, and types of its formal parameters.
Subprogram protocol is its parameter profile plus, if it is a function, its return type. In languages in which subprograms have types, those types are defined by the subprogram’s protocol.
8. What are formal parameters? What are actual parameters?
Answer: Formal parameters are the parameters in the subprogram header.
Actual parameters are a list of parameters to be bound to the formal parameters of the subprogram which must be included with the name of the subprogram by the subprogram call statements.
10 . What are the differences between a function and a procedure ?
Answer:
- Functions return values while procedure does not .
- Procedure defines new statements, while function define new user-defined operators.
22. Which ristriction are called bounds?
Answer: Restriction in Java that specify the range of classes that can pass to the generic method as generic parameters.
24. What is an overloaded subprogram?
Answer: Overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. Every version of an overloaded subprogram must have a unique protocol; that is, it must be different from the others in the number, order, or types of its parameters, and possibly in its return type if it is a function.
32. What exactly is a delegate?
Answer: A delegate is the power and flexibility of method pointers in C# increased by making them objects.
34. What is a closure?
Answer: Closure is a nested subprogram ant its referencing environment, which together allow the subprogram to be called from anywhare in a program
37. In what way coroutine different from conventional subprogram?
Answer: coroutine control mechanism is often called symmetric unit control model, in the otherhand conventional subprogram have a master-slave relationship between a caller and a called subprogram.
coroutine can have multiple entry point which are controlled by the coroutine themselves.
problem set
1. What are arguments for and against a user program building additional definitions for existing operators, as can be done in Python and C++? Do you think such user-defined operator overloading is good or bad ? Support your answer.
Answer:
I think it is good. User program building addition definition can give an access for those who wants to make their own specific datatype and also their custom operator.
User define operator overloading is good I think, because the user can custom their own operator overloading as long as it does not make them confused.
3. Argue in support of the template functions of C++. How is it different from the template functions in other languages?
Answer: C++ templated classes are instantiated to become typed classes at compile time. For example, a templated Count class, can be created with the following declaration:
Count<int> doCount;
5.Consider the following program writen in C syntax:
void swap(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}
void main() {
int value = 1, list[5] = {2, 4, 6, 8, 10};
swap(value, list[0]);
swap(list[0], list [1]);
swap(value, list[value]);
}
For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap ?
a. Passed by Value
value =1 , list[5] = {2,4,6,8,10}
b. Passed by reference
value =6, list[5] ={4,1,2,8,10}
c. Passed by value-result
value =6, list[5] ={4,1,2,8,10}
7. Consider the following program written in C syntax:
void fun (int first, int second){
first += first;
second += second;
}
void main() {
int list [2] = {3,5};
fun(list[0], list[1]);
}
For each of the following parameter-passing methods, what are the values of the list array after execution?
Answer:
a. Passes by value : 3, 5
b. Passes by reference : 6, 10
c. Passes by value-result : 6, 10
8 . Argue against the Java design of not providing operator overloading
Answer: Arithmetic operators are often used for more than one purpose. For example, + usually is used to specify integer addition and floating-point addition. Some languages—Java, for example—also use it for string catenation. This multiple use of an operator is called operator overloading and is generally thought to be acceptable, as long as neither readability nor reliability suffers.
Special thanks to Mr. Tri Djoko Wahjono, Ir., M.Sc.
CONCEPTS OF PROGRAMMING LANGUAGES TENTH EDITION Robert W. Sebesta Chapter 8 - Statement-Level Control Structure
CONCEPTS OF
PROGRAMMING LANGUAGES
TENTH EDITION
Robert W. Sebesta
Chapter 8 - Statement-Level Control Structure
1. What is the definition of control structure?
Answer: A control structure is a pack of control statement and collection of statements that control its execution.
2. What did Bohm and Jocopini prove about flowcharts?
Answer: Bohm and Jocopini could prove that all algorithms can be expressed by flowchart and alson can be coded in a programming language with only two control statement (choosing between two control and logically controlled iteration)
3. What is the definition of block?
Answer: Block is a squence of code or reserved word that is used by the programmer to give an istruction.
5. What are the design issues for selection structures?
The design issues for selection structures are:
Answer:
- What is the form and type of the expression that controls the selection?
- How are the then and else clauses specified?
- How should the meaning of nested selectors be specified?
9. What are the design issues for multiple-selection statements?
Answer:
What is the form and type of the expression that controls the selection?
How are the selectable segments specified?
How are the case values specified?
How should unrepresented selector expression values be handled, if at all?
Is execution flow through the structure restricted to include just a single selectable segment?
14. What are the design issues for all iterative control statements?
Answer:
How is the iteration controlled?
Where should the control mechanism appear in the loop statement?
15. What are the design issues for counter-controlled loop statements?
Answer:
What are the type and scope of the loop variable?
Should it be legal for the loop variable or loop parameters to be changed in the loop, and if so, does the change affect loop control?
Should the loop parameters be evaluated only once, or once for every iteration?
20. What contemporary languages do not include a goto?
Answer:
Java language is the contemporary language that doesn’t include a goto.
21. What are the design issues for logically controlled loop statements?
Answer:
Should the control be pretest or post-test?
Should the logically controlled loop be a special form of a counting loop or a separate statement?
23. What are the design issues for user-located loop control mechanisms?
Answer:
Should the conditional mechanism be an integral part of the exit?
Should only one loop body be exited, or can enclosing loops also be exited?
Problem Set
1. What design issues should be considered for two-way selection statements?
Answer:
The design issues for two-way selectors can be summarized as follows:
• What is the form and type of the expression that controls the selection?
• How are the then and else clauses specified?
• How should the meaning of nested selectors be specified?
11. Explain the advantages and disadvantages of the Java switch statement, compared to C++’s switch statement.
Answer: Integral ( byte, short etc.), char and String data types can be used as an argument in Java's switch statement, in the other hand in C++ the argument can be int or char.
14. State one of the main legitimate needs for gotos.
Answer: To make a loop exit statement.
Special thanks to Mr. Tri Djoko Wahjono, Ir., M.Sc.
Langganan:
Komentar (Atom)