Q1. For the following grammar: S a S b c E E d f E a

Similar documents
J a v a Quiz (Unit 3, Test 0 Practice)

Java from a C perspective. Plan

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Java Interview Questions and Answers

How To Program In Scheme (Prolog)

Moving from CS 61A Scheme to CS 61B Java

AP Computer Science Java Subset

Semantic Analysis: Types and Type Checking

Chapter 15 Functional Programming Languages

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

CS 111 Classes I 1. Software Organization View to this point:

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

C++ INTERVIEW QUESTIONS

Object-Oriented Programming in Java

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

public static void main(string[] args) { System.out.println("hello, world"); } }

Part I. Multiple Choice Questions (2 points each):

Programming Languages CIS 443

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

C++FA 5.1 PRACTICE MID-TERM EXAM

Semester Review. CSC 301, Fall 2015

C++ Overloading, Constructors, Assignment operator

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Object Oriented Software Design II

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Basic Lisp Operations

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Lecture Data Types and Types of a Language

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Glossary of Object Oriented Terms

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Functional Programming

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Fundamentals of Java Programming

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

COMP 110 Prasun Dewan 1

Compiler Construction

1 Abstract Data Types Information Hiding

Sample CSE8A midterm Multiple Choice (circle one)

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Sources: On the Web: Slides will be available on:

Chapter 5 Names, Bindings, Type Checking, and Scopes

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Introducing Variance into the Java Programming Language DRAFT

Java CPD (I) Frans Coenen Department of Computer Science

C Compiler Targeting the Java Virtual Machine

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

The C Programming Language course syllabus associate level

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

The Cool Reference Manual

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Massachusetts Institute of Technology 6.005: Elements of Software Construction Fall 2011 Quiz 2 November 21, 2011 SOLUTIONS.

D06 PROGRAMMING with JAVA. Ch3 Implementing Classes

Introduction: Abstract Data Types and Java Review

CMSC 202H. ArrayList, Multidimensional Arrays

Basic Object-Oriented Programming in Java

Java Programming Fundamentals

D06 PROGRAMMING with JAVA

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

Remote Method Invocation

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

I. INTRODUCTION. International Journal of Computer Science Trends and Technology (IJCST) Volume 3 Issue 2, Mar-Apr 2015

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

CSCI 3136 Principles of Programming Languages

No no-argument constructor. No default constructor found

Integrating Formal Models into the Programming Languages Course

Crash Course in Java

13 Classes & Objects with Constructors/Destructors

Object Oriented Software Design

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Programming Database lectures for mathema

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Computational Mathematics with Python

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Introduction to Object-Oriented Programming

The Interface Concept

Description of Class Mutation Mutation Operators for Java

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Building a Multi-Threaded Web Server

Iterators. Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.

Konzepte objektorientierter Programmierung

Parameter passing in LISP

Transcription:

Chapter 3 (3.1 3.3) Describing Syntax and Semantics Chapter 4 Lexical and Syntax Analysis Chapter 5 Names, Binding, Type Checking and Scopes Chapter 6 Data Types Chapter 7 Expressions and Assignment Statements Chapter 8 (8.1 8.4) Statement-Level Control Structures Chapter 9 (9.1 9.10) Subprograms Chapter 10 Implementing Subprograms Chapter 11 Abstract Data Types and Encapsulation Constructs Chapter 12 (12.1, 12.2, 12.3, 12.5, 12.6) Support for Object - Oriented Programming Chapter 14 (14.1, 14.3, 14.4) Exception Handling Chapter 15 (15.1 15.5) Functional Programming Languages Q1. For the following grammar: S a S b c E E d f E a (1) Circle the strings that can be generated by the grammar and cross out those that cannot: cfda (T) acfab (F) aacdbb (T) aacfdab (F) (2) The grammar is ambiguous. True or false (circle one) false 1

Q2. const member functions... i. are mutator functions ii. must have a void return type iii. cannot change its object's data members iv. can invoke any other member functions in its class Q3. Consider the following program in C: int x=0; int p(int y, int z) { x=x+1; y=y+1; z=z+1; printf ( %d, x+y+z); void main(){ int x=1; p(x, x); Passing by value: 5 Passing by reference: 7 int p(int &y, int &z) x y z 1 2

Q4. Write a lambda function in Scheme that takes one argument and returns the value of that argument plus itself. (lambda (x) (+ x x)) Q5. Assuming that the following definitions are executed in this order: (define a 6) (define b (3 14 27)) (define c (cons a (cdr b))) What is the result of typing the following into the Scheme interpreter: c =>??? ( 6 14 27 ) (car (cdr c)) =>??? 1 Q6. Write a Scheme function that delete all a given atom from a given list. CONS builds a list from its two arguments. CAR returns the first element of that list. CDR returns the list after removing its first element (DEFINE (deleteall atm lst) (COND Assume: ((NULL? lst) '()) (define lst (3 4 14 3 27)) ((EQ? atm (CAR lst)) (deleteall atm (CDR lst))) (define atm 3) (ELSE (CONS (CAR lst) (deleteall atm (CDR lst))) )) (4 (deleteall 3 (14 3 27))) (4 14 (deleteall 3 (3 27)) (4 14 27 (deleteall 3 ())) 3

Q7. What dangers are avoided in Java by having garbage collection, relative to C++? Answer: garbage collection removes the necessity of allowing users to deallocate objects, thereby eliminating the possibility of user-created dangling pointers. Q8. What is the advantage of binding things as early as possible? Is there any advantage to delaying binding? Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility Q9. In some languages, the user manages the heap. Why is this potentially dangerous? For example in C++ the user manages the heap by explicitly requesting memory from the heap via new and releasing it by delete. Two common problems that occur are: (1) dangling pointers: the user releases memory back to the heap too early or rather while there are still references to that memory in their program that may be accessed in the future. (2) memory leaks: when the user neglects to return memory to the heap when it is no longer needed, may cause you to eventually run out of memory on the heap. 4

10. Consider the following C++ code that contains a memory leak: void foo(){ int *x; while (1) x = new int; Rewrite this code by inserting a delete operator at the appropriate place to eliminate the memory leak. Do not change the code otherwise simply add the appropriate number of delete statement(s) in the appropriate place(s). Solution: void foo(){ int *x; while (1){ x = new int; delete x; 5

Q11. Consider the Fibonacci series of numbers, where each number in the series is the sum of the preceding two: 0, 1, 1, 2, 3, 5, 8, 13, 21, We can define the Fibonacci function in mathematical notation in the following way: 0 if n = 0 Fib( n) = 1 if n = 1 Fib( n 1) + Fib( n 2) otherwise Define a Scheme procedure Fib that implements this function. (define (fib n) (cond ((= n 0) 0) ((= n 1) 1) else (+ (fib (- n 1)) (fib (- n 2)))))) 6

Q12. What will be the output if we run the following program? public class Foo { public static void main(string[] args) { int[] a = {9, 11; System.out.println( a[0] ); try { if(a[1] > 10 && a[1] < 15) { throw new RuntimeException(); System.out.println( a[1] ); catch( RuntimeException e ) { System.out.println("inside catch"); System.out.println("program ends"); 7

Q13. In C++, how is a reference different from a pointer? Syntactically, a reference is treated as a regular variable. Moreover, once a reference is initialized to refer to a certain memory location, future assignments will keep the reference referring to the same place, whereas a pointer can be made to point somewhere else. Q14 Consider the following Java program. class Base { void m() { System.out.println("Base.m()"); void n() { System.out.println("Base.n()"); class Der1 extends Base { void m() { System.out.println("Der1.m()"); class Der2 extends Base { void n() { System.out.println("Der2.n()"); class Main{ static void p(base x) { x.m(); x.n(); public static void main(string[] args) { p(new Base()); p(new Der1()); p(new Der2()); What does this program print? Example solutions Base.m() Base.n() Der1.m() Base.n() Base.m() Der2.n() 8

15. Draw all possible parse trees for the string aaa using this grammar: <S> ::= a <S> <S> a a 16. Consider the set of strings that consist of zero or more a s, followed by zero or more b s, followed by zero or more c s, followed by zero or more d s, such that the number of a s equals the number of b s, and also the number of c s equals the number of d s. For example, string aabbcccddd is in this set because it has 2 a s, 2 b s, 3 c s, 3 d s. Write an unambiguous BNF grammar for this language. <S> ::= <X> <Y> <X> ::= a <X> b <empty> <Y> ::= c <Y> d <empty> 9

17. Discuss the tradeoffs between providing an explicit memory deallocation operator and providing a complete and correct implementation of garbage collection. Garbage collection is computationally expensive and sometimes unnecessary. Explicit deallocation allows the programmer to control when deallocation is performed and is generally less expensive than garbage collection. However, explicit deallocation also allows for programmer errors in terms of dangling pointers and memory leaks. 18. T/F Two primary reasons for subclassing are to achieve code-reuse or polymorphism. Answer 1: True. There are other reasons, but usually you subclass because you've found a class that already does almost what you want to do (reuse) or you want to be able to treat all subclasses in terms of their parent (polymorphism). 19 In C++, static methods and data members are only accessible from static methods. Static methods and data cannot be accessed from non-static methods. Answer: False. A non-static method can call a static one. 10

20. The compiler provides your class with a built-in no-argument constructor, but once you define any constructor, the built-in one is no longer generated. Answer: True. Even if you only write a copy constructor, the no-argument constructor will go away. You can define multiple constructors to provide optional ways to create and initialize instances of the class. class Test { int i; boolean b=true; Test(int j, boolean a){ i = j; b = a; Test(int j){ i = j; int get(){ return i; This constructor initializes the two properties to the values passed as arguements. A second constructor passes an initial value to just one of the variables. 11

If a class includes one or more explicit constructors, the java compiler does not create the default constructor. So for the class shown above, the code Test ex = new Test(); will generate a compiler error stating that no such constructor exists. 21. If a function provides a throw list but doesn't include any exceptions in the list, then it can safely throw any exception. In other words, the following function can throw any exception without invoking the unexpected handler: void tomservo(int i) throw() { [etc] Answer: False. An empty throw list means that you won't throw anything. If you omit the throw list entirely, that means you can throw anything. 12

22. /* Represents a square matrix. */ typedef struct squarematrix { int n; /* The dimension of this matrix. */ SquareMatrix; /* Returns the element A i,j. */ int get(const SquareMatrix A, const int i, const int j) { /* Implementation omitted. */ Using those fragments, complete the function issymmetric below, which takes as arguments a square matrix A and returns true if A is symmetric, false otherwise. int issymmetric(const SquareMatrix A) { int i, j; for (i = 0; i!= A.n; i++) { for (j = i+1; j < A.n; j++) { if (get(a, i, j)!= get(a, j, i)) { return 0; return 1; 13