Principles of Programming Language Midterm

Principles of Programming Language Midterm

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

What is a recursive function?

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

1

All-time users

1

Favorites

0

Last updated

1 year ago

Date created

Mar 14, 2020

Cards (24)

Section 1

(24 cards)

What is a recursive function?

Front

When a function is called within the same function, it is known as recursion in C++. The function which calls the same function, is known as recursive function.

Back

What is malloc and calloc?

Front

Malloc stands for memory allocation, and calloc stands for contiguous allocation. The name malloc and calloc() are library functions that allocate memory dynamically. It means that the size of a data structure (i.e. memory allocated) is changed during the runtime.

Back

What types of programming paradigms are there?

Front

There are two types: 1. Imperative Programming Paradigm (is a programming paradigm that uses statements that change a program's state) - Procedural programming paradigm - Object oriented programming - Parallel processing approach 2. Declarative Programming Paradigm (is a programming paradigm in which the programmer defines what needs to be accomplished by the program without defining how it needs to be implemented. In other words, the approach focuses on what needs to be achieved instead of instructing how to achieve it.) - Logic programming paradigm - Functional programming - Database processing approach Declarative Programming is like asking your friend to draw a landscape. You don't care how they draw it, that's up to them. Imperative Programming is like your friend listening to Bob Ross tell them how to paint a landscape. While good ole Bob Ross isn't exactly commanding, he is giving them step by step directions to get the desired result. List<int> results = new List<int>(); foreach(var num in collection) { if (num % 2 != 0) results.Add(num); } var results = collection.Where( num => num % 2 != 0);

Back

Write recursive code to find factorials in c++

Front

#include<iostream> using namespace std; int main() { int factorial(int); int fact,value; cout<<"Enter any number: "; cin>>value; fact=factorial(value); cout<<"Factorial of a number is: "<<fact<<endl; return 0; } int factorial(int n) { if(n<0) return(-1); /Wrong value/ if(n==0) return(1); /Terminating condition/ else { return(n*factorial(n-1)); } }

Back

What is a programming paradigm?

Front

- The term programming paradigm refers to a style of programming. - It does not refer to a specific language, but rather it refers to the way you program. - There are lots of programming languages that are well-known but all of them need to follow some strategy when they are implemented. And that strategy is a paradigm.

Back

What is considered as 1 token?

Front

1. strings e.g. "hello world" 2. numbers e.g. 1234, 12.34 3. symbols e.g. >> ++ - 4. separators e.g. , ; 5. keywords e.g. for, if 6. identifier e.g. variable name, function name

Back

Write iterative function to find factorial.

Front

int factorialUsingIteration(int n) { int res = 1, i; // using iteration for (i = 2; i <= n; i++) res *= i; return res;

Back

What are the criteria for a good learning language?

Front

1. Readability - Understand and comprehend a computation easily and accurately 2. Write-ability - express a computation clearly, correctly, concisely, and quickly 3. Reliability - assures a program will not behave in unexpected or disastrous ways 4. Orthogonality - A relatively small set of primitive constructs can be combined in a relatively small number of ways - Every possible combination is legal - Lack of orthogonality leads to exceptions to rules Note: Orthogonality" Changing A does not change B". An example of an orthogonal system would be a radio, where changing the station does not change the volume and vice-versa. 5. Uniformity - similar features should look similar and behave similar 6. Maintainability - errors can be found and corrected and new features added easily 7. Generality - avoid special cases in the availability or use of constructs and by combining closely related constructs into a single more general one 8. Extensibility - provide some general mechanism for the user to add new constructs to a language 9. Standardability - allow programs to be transported from one computer to another without significant change in language structure 10. Implementability - ensure a translator or interpreter can be written

Back

What is tokenization?

Front

A compiler converts high-level language (HLL) to low-level language (LLL). Basically, it converts a human understand language into machine code. Lexical Analysis is the first phase in a compiler, and it converts the High level input program into a sequence of Tokens. The output is a sequence of tokens that is sent to the parser for syntax analysis

Back

What is passing by value?

Front

Passing by value means that the value of the function parameter is copied into another location of your memory, and when accessing or modifying the variable within your function, only the copy is accessed/modified and the original value is left untouched. Passing by value is how your values are passed on most of the time.

Back

Steps to converting code to tokens

Front

To convert a code into tokens it requires the following steps. Step 1: Separate lexeme into tokens. Step 2: Remove lines. Step 3: Remove comments. Step 4: Remove blank spaces.

Back

What is a scope?

Front

A scope is a region of the program. Scope refers to the visibility of variables and methods in one part of a program to another part of that program. There are three places, where variables can be declared: 1. Inside a function or a block which is called local variables 2. In the definition of function parameters which is called formal parameters. 3. Outside of all functions which is called global variables.

Back

What is memory management

Front

Memory management is the process of controlling and coordinating computer memory, assigning portions called blocks to various running programs to optimize overall system performance

Back

What is a heap?

Front

- Memory is allocated during execution of instructions written programmers. - Called heap because it is a pile of memory space available to programmers to allocate and deallocate.

Back

What is a data type?

Front

1. It is a reserved word that is known by a compiler. 2. Each data type has memory to be allocated in the random-access memory (RAM) and a range of values.

Back

What is calloc()?

Front

1. "calloc" or "contiguous allocation" method is used to dynamically allocate the specified number of blocks of memory of the specified type. It initializes each block with a default value '0'.

Back

What is malloc()?

Front

1. "malloc" or "memory allocation" method is used to dynamically allocate a single large block of memory with the specified size. 2. It returns a pointer of type void which can be cast into a pointer of any form.

Back

What is passing by reference?

Front

Passing by reference means that the memory address of the variable (a pointer to the memory location) is passed to the function. This is unlike passing by value, where the value of a variable is passed on. In the examples, the memory address of myAge is 106. When passing myAge to the function increaseAgeByRef, the variable used within the function (age in this example) still points to the same memory address as the original variable myAge (Hint: the & symbol in front of the function parameter is used in many programming languages to get the reference/pointer of a variable).

Back

What is external fragmentation?

Front

External fragmentation arises when there are many small gaps between allocated memory blocks, which invalidates their use for an allocation request.

Back

What are parenthesis'?

Front

1. Sometimes it refers to a bracket. 2. It has an opening and a closing punctuation marks. 3. Parentheses that are used in C++ included { }, [ ], ( ), < >, ' ', and " ".

Back

What usage does parenthesis have in C/C++?

Front

1. Curly bracket, { } - It groups statements into a meaningful and manage-able way. 2. Square bracket, [ ] - It used to initialize a set of elements or items. 3. Round bracket, ( ) - It is used to indicate one or more arguments for a function. 4. Angle quotation, < > - It is used to indicate a template for a data type. 5. Single quotation, ' ' - It is used to indicate a character. 6. Double quotation, " " - It is used to indicate a string of characters.

Back

What is a pointer?

Front

1. A pointer is a variable, which consists of an address and a value. 2. It is used to refer to an address for another variable. 3.*, dereferencing operator retrieves value for a pointer that it is pointing to. 4. &, ampersand referencing operator retrieves the address for a pointer that it is pointing to.

Back

What is the difference between iteration and recursion?

Front

A program is called recursive when an entity calls itself. A program is call iterative when there is a loop (or repetition), and a set of instructions is repeatedly executed.

Back

What are the sizes of data types in the RAM?

Front

bool = 1 char = 1 int = 4 float = 4 double = 8

Back