Quiz
programming languages midterm
This quiz has question types that open in the usual player.
Questions in this quiz (123)
1What is a programming language?
a notational system for describing computation in human-readable and machine-readable form- ATrue
- BFalse
2Is HTML a programming language? no.
a programming language must be Turing complete meaning it has integer variables, arithmetic and sequentially executes statements, selection (if), and loop (while) statements, and has a mechanism for defining new functions using existing functions, selection, and recursion- ATrue
- BFalse
3What are some of the concepts of a programming language?
- Aparameter passing (by-value, by-reference)
- Btyping (static or dynamic)
- Csupport for abstraction (procedural, data)
- Dscope (static or dynamic)
- Eimplementation (interpreted or compiled)
- Freadability (language)
- Greliability (safety)
4[blank_start]C vs. C++ vs. C#[blank_end] for systems programming
[blank_start]Fortran vs. C[blank_end] for numerical computations
[blank_start]PHP vs. Ruby[blank_end] for web-based applications
Ada vs. C for embedded systems
Common Lisp vs. Scheme vs. ML for symbolic data manipulation
[blank_start]Java vs. .NET[blank_end] for networked PC programs- AC vs. C++ vs. C#
- BFortran vs. C
- CPHP vs. Ruby
- DJava vs. .NET
5Programming languages have four properties:
- ASyntax
- BNames
- CTypes
- DSemantics
- ELogic
- FVariables
- GFunctions
6The syntax of a programming language is a precise description of all its grammatically correct programs
- ATrue
- BFalse
7Named entities are bound in a running program to:
- Ascope
- Bvisibility
- Ctype
- Dlifetime
- Eglobal
- Fsyntax
8A type is a collection of values and a collection of operations on those values
Simple types - numbers, characters, booleans
Structured types - Strings, lists, trees, hash tables- ATrue
- BFalse
9Type system in a programming language is needed to organize data and helps to check the correctness of programs:
Determine legal operations
Detect type errors- ATrue
- BFalse
10What is a paradigm?
a model or world view
What is a model?
a simplified view of something in the real world
What is a programming language paradigm?
a pattern for a programming language to follow- ATrue
- BFalse
11classify programming languages into two categories:
Imperative in which the programmer instructs the machine how to change its state
Declarative in which the programmer merely declares properties of the desired result, but not how to compute it- ATrue
- BFalse
12Imperative languages include:
procedural
object-oriented
Declarative languages include:
functional
logical
mathematical- ATrue
- BFalse
13There are four main programming paradigms:
- AImperative
- BObject-Oriented (OO)
- CFunctional
- DLogic (Declarative)
- EMathematical
- FProdecdural
14An OO Program is a collection of objects that interact by passing messages that transform the state (Java, C++, C#, Python etc)
OO includes:
Encapsulation
Inheritance
Polymorphism- ATrue
- BFalse
15Functional programming models a computation as a collection of mathematical functions (F#, Lisp, Scheme, ML, Haskell etc)
Includes:
Functional composition
Recursion- ATrue
- BFalse
16Logic / Declarative programming declares what outcome the program should accomplish, rather than how it should be accomplished (Prolog)
- ATrue
- BFalse
17Key characteristics of successful languages:
Simplicity and readability
Clarity about binding
Reliability
Support
Abstraction
Orthogonality
Efficient implementation- ATrue
- BFalse
18A language element is bound to a property at the time that property is defined for it.So a binding is the association between an object and a property of that object
- ATrue
- BFalse
19Early vs Late binding:
early binding takes place at compile-time
late binding takes place at run time- ATrue
- BFalse
20Static vs Dynamic binding:
static binding happens prior to execution (i.e. compile-time)
dynamic binding happens and is changeable during run-time (i.e. execution)- ATrue
- BFalse
21A language is reliable if: (4)
- AProgram behavior is the same on different platforms
- BType errors are detected
- CSemantic errors are properly trapped
- DMemory leaks are prevented
- EThe language has good readability
- FSimple and easy to follow
22Data abstractions: simplify the behavior and attributes of data for humans
- ATrue
- BFalse
23Control abstractions: simplify properties of the transfer of control
- ATrue
- BFalse
24A language is orthogonal if its features are built upon a small, mutually independent set of primitive operations
- ATrue
- BFalse
25Compiled vs Interpreted:
Compiled languages are translated into machine code that can be run directly on a computer’s processor
Interpreted languages are processed by a higher-level virtual machine- ATrue
- BFalse
26Pure Interpretation
Interpreter stays around for the execution of the program
Interpreter is the focus of control during execution- ATrue
- BFalse
27Compiled vs Interpreted advantages:
Interpretation:
Greater flexibility
Better diagnostics (error messages)
Compilation:
Better performance- ATrue
- BFalse
28Most language implementations include a mixture of both compilation and interpretation
- ATrue
- BFalse
29comma operator - evaluates expressions from left to right
x = ( y = 3, m =2, h = 5, h+1 );
actually is:
y = 3
m = 2
h = 5
x = h + 1
The last expression is the value of the overall expression- ATrue
- BFalse
30all functions pass by value. Means that a copy of the value is made and placed on the stack for the function to use
To change the variable in the calling function you have to pass the address of the variable (&var). This is typically called “pass by reference”. A copy of the address is made and passed on the stack and the function uses this to change the original variable- ATrue
- BFalse
31Phases of Compilation

- Ayes
- B-
32What is a formal language?
set of sentences (strings) over some alphabet
legal strings = sentences
How do we define a formal language?
grammar (defines syntax of a language)
There are finite and infinite languages
A language is a set of strings. It is finite if it has a finite number of strings in it.
Most interesting languages are infinite- ATrue
- BFalse
33Syntax is the structure of a language
- ATrue
- BFalse
34Lexical: name of some identifier typed incorrectly
Syntactical: missing semicolon or unbalanced parenthesis
Semantical: incompatible value assignment
Logical: code not reachable, infinite loop- ATrue
- BFalse
35syntatically valid means:
the grammatical arrangement of words in the very sentence is correct
ie. Socrates is a man, is syntatically correct- ATrue
- BFalse
36Lexically correct means:
each word of the sentence is spelled correctly
ie. Socrates is a Mann, is lexically incorrect- ATrue
- BFalse
37Semantically correct means:
the sentence/word is properly assigned
ie. Man Socrates is a, is semantically incorrect
Socrates is a man, is semantically correct- ATrue
- BFalse
38Tokens generally fall into several categories:
Identifiers
Literals or constants
Reserved words (or keywords)
Special symbols, such as “;” m “<=“ “+”- ATrue
- BFalse
39Most modern text editors use regular expressions in text searches
- ATrue
- BFalse
40Free-format language: one in which format has no effect on program structure other than satisfying the principle of longest substring
Fixed format language: one in which all tokens must occur in prespecified locations on the page- ATrue
- BFalse
41Context-free grammar consists of a series of grammar rules
Nonterminals: names for phrase structures, since they are broken down into further phrase structures
Terminals: words or token symbols that cannot be broken down further (example: single digit)- ATrue
- BFalse
42The notation for context-free grammars (CFG) is sometimes called Backus-Naur Form (BNF)
It consists of:
–A set of terminals T
–A set of non-terminals N
–A start symbol S (a non-terminal)
–A set of productions- ATrue
- BFalse
43Derivation: the process of building in a language by beginning with the start symbol and replacing left-hand sides by choices of right-hand sides in the rules
Example:
noun → girl | dog
verb → sees | pets
→ separates left and right sides
| indicates a choice- ATrue
- BFalse
44Some problems with context free grammar:
-A legal sentence does not necessarily make sense.
-Positional properties (such as capitalization at the beginning of the sentence) are not represented.
-Grammar does not specify whether spaces are needed.
-Grammar does not specify input format or termination symbol- ATrue
- BFalse
45A grammar is context-free when nonterminals appear singly on the left sides of productions
Anything not expressible using context-free grammars is a semantic, not a syntactic, issue- ATrue
- BFalse
46Syntax establishes structure, not meaning
- ATrue
- BFalse
47Parse tree

- Ayes
- B-
48Ambiguous grammar: one for which two or more distinct parse or syntax trees are possible
- ATrue
- BFalse
49A context-free grammar (CFG) is a generator for a context-free language (CFL):
It describes the strings of tokens that are syntactically legal.
It describes how a parser must act to parse correctly- ATrue
- BFalse
50Parser generator: a program that automates top-down or bottom-up parsing
Top-down parser: expands nonterminals to match incoming tokens and directly construct a derivation
Bottom-up parsing: constructs derivations and parse trees from the leaves to the roots- ATrue
- BFalse
51What is Data?
Machine data carries no type information.
Basically, just a sequence of bits (0100)- ATrue
- BFalse
52Data type: the basic concept underlying the representation of data in programming languages
- ATrue
- BFalse
53Predefined types: those types supplied with a language, from which all other types are constructed
Simple types: have no other structure than their inherent arithmetic or sequential structure- ATrue
- BFalse
54Type system: methods for constructing types, the type equivalence algorithm, type inference rules, and type correctness rules
- ATrue
- BFalse
55Types:

- Ayes
- B-
56In mathematics, integer set is infinite
In programs, there is always a largest and smallest integer- ATrue
- BFalse
57Examples of data structures in C: (5)
- AArray
- BStack
- CQueue
- DLinked List
- ETree
- FType
- GToken
58Data structures and algorithms are the foundation of computer programming
- ATrue
- BFalse
59An Abstract Data Type (ADT) is a data type together with the operations
Examples:
List with operations insert and delete
Stack with operations push and pop- ATrue
- BFalse
60A data structure strives to provide many useful, efficient operations
Downsides:
Time vs. space
One operation more efficient if another less efficient
Generality vs. simplicity vs. performance- ATrue
- BFalse
61In Imperative paradigm programmer instructs the machine how to change its state
- ATrue
- BFalse
62What NOT to measure in algorithmic complexity?
- ACPU Time
- BMemory
- CNumber of steps
- DNumber of particular operations
- ENumber of disk operations
- FNumber of network packets
- GAsymptotic complexity
- HComplexity time
63Worst-case = An upper bound on the running time for any input of given size
Average-case = Assume all inputs of a given size are equally likely
Best-case = The lower bound on the running time(the optimal case)- ATrue
- BFalse
64Algorithm complexity is a rough estimation of the number of steps performed by given computation depending on the sizeof the input data
Linear complexity O(n)–all elements are processed once (or constant number of times)
Quadratic complexity O(n^2) –each of the elements is processed n times- ATrue
- BFalse
65The compiler uses function prototypes to validate function calls.
- ATrue
- BFalse
66In a von Neumann machine memory holds:
Instructions
Data
Intellectual heart: assignment statements
Others (Conditional branching, goto)- ATrue
- BFalse
67An imperative language is said to be Turing Complete if it provides an effective basis for implementing any algorithm that can be designed
- ATrue
- BFalse
68Procedural abstraction allows the programmer to be concerned mainly with a function interface, ignoring the details of how it is computed
- ATrue
- BFalse
69Expression: returns a value and produces no side effect
Statement: executed for its side effects and returns no value- ATrue
- BFalse
70Copy semantics: Expression is evaluated to a value (value-oriented languages)
Reference semantics: Expression is evaluated to an object, whose pointer (reference) is copied to the target (reference-oriented languages)- ATrue
- BFalse
71Side Effects (generally assignment statements )
a side effect is some permanent state change caused by execution of a function
happens when a procedure changes a variable from outside its scope- ATrue
- BFalse
72Expression-oriented vs. Statement-oriented
Expression-oriented: functional languages (Lisp, Scheme, ML)
Statement-oriented: most imperative languages
C halfway in-between- ATrue
- BFalse
73Functions and Procedures: blocks whose execution is deferred and whose interfaces are clearly specified
Functions should produce a value only and have no side effect (functions return a value)
Procedures produce no values and operate by producing side effects (procedures assign values)- ATrue
- BFalse
74An argument is an expression that appears in a function call. A(a, b)
A parameter is an identifier that appears in a function declaration A(int x, int y)- ATrue
- BFalse
75All arguments in C are passed by value
- ATrue
- BFalse
76Static variables are:
data objects of any type which can be declared as local or global.
Static local variables are initialized at the beginning of the program and are "active" at all times during the execution of the program- ATrue
- BFalse
77Implicit conversion:
Implicit conversions do not require any operator for converted. They are automatically performed when a value is copied to a compatible type in the program:
int i=20;
double p = i;- ATrue
- BFalse
78Explicit type casting:
In C language, Many conversions, especially those that imply a different interpretation of the value, require an explicit conversion
int i=20;
short p = (short)i;- ATrue
- BFalse
79Explicit typecasting is a must in integer division
- ATrue
- BFalse
80The detection of type errors, either at compile time or at run time, is called type checking
- ATrue
- BFalse
81A Type System has rules for
–type equivalence (when are the types of two values the same?)
–type compatibility (when can a value of type A be used in a context that expects type B?)
–type inference (what is the type of an expression, given the types of the operands?)- ATrue
- BFalse
82 Program is valid if
its Declarations are valid and its Block body is valid with respect to the type map for those Declarations- ATrue
- BFalse
83STRONG TYPING
means that the language prevents you from applying an operation to data on which it is not appropriate
STATIC TYPING
means strongly typed and that the compiler can do all the checking at compile time- ATrue
- BFalse
84A language is statically typed if the types of all variables are fixed when they are declared at compile time.
A language is dynamically typed if the type of a variable can vary at run time depending on the value assigned- ATrue
- BFalse
85Strongly typed:
language that specifies a statically applied type system that guarantees all data-corrupting errors will be detected at the earliest possible point.
Weakly-typed:
language without static type systems. It has loopholes that may allow unsafe programs.
Untyped:
language where values are chunks of bits- ATrue
- BFalse
86C is strongly typed
- ATrue
- BFalse
87#define pre-processor statement:
used to define constants and macros.
Replacement occurs before the program is compiled. However, no replacement occurs within strings
#define TRUE 1- ATrue
- BFalse
88#include pre-processor statement substitutes the #include line with the contents of the file before the program is compiled
- ATrue
- BFalse
89You can initialize all elements:
int n[5] = { 2, 7, 3, 5, 9 };
If not enough initializers, rightmost elements become 0.
Example: n[4] element will be initialized to zero:•int n[5] = { 2, 7, 3, 5 };
You can initialize all elements to 0 (best practice, only works with 0 value!):
int n[5] = { 0 };
If size omitted, initializers determine it (5 initializers -> size is 5):
int n[] = { 2, 7, 3, 5, 9 };- ATrue
- BFalse
90Pointer is a variable that contains the address of another memory location
& - “address of” operator : returns the memory address of a variable
* - “indirection” operator : returns the content stored at this address- ATrue
- BFalse
91int x, y, *p;
x = 3; /* assign value (integer) to variable x*/
p = &x; /* assign value (memory address of x) to variable p*/
y = *p; /* assign value (value where p points to) to variable x*/
Notation:
&x /* address of x */
p /* memory address stored in p (pointed to by p) */
*p /* contents of the memory whose address is in p*/- ATrue
- BFalse
92Size of pointer (for 64 bit systems) is 8 bytes
32 bit systems are 4 byte- ATrue
- BFalse
93%p used in printf() to output the value of a pointer.
- ATrue
- BFalse
94It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned
int *ptr = NULL:
Initializing a pointer to 0 is equivalent to initializing a pointer to NULL, but NULL is preferred.
The value 0 is the only integer value that can be assigned directly to a pointer variable.- ATrue
- BFalse
95Pointers should be initialized either when they’re defined or in an assignment statement
- ATrue
- BFalse
96int x=5, *p=&x;
*&x - The contents of the location whose address is the address of x, ie. 5
&*p - The address of the contents of the location whose address is in p. ie. &x
&p - The address, where value of pointer is kept (pointer to a pointer)- ATrue
- BFalse
97Arrays and pointers are intimately related in C and often may be used interchangeably.
An array name can be thought of as a constant pointer- ATrue
- BFalse
98int a[5]={3,13,17,23,53};
int* p;
a -same as &a[0]
*a -same as a[0]
*a=12; /* is the same as a[0]=12; */
p=a; /* same as p=&a[0]; */
*p /* same as a[0] */
*(p+2) /* same as a[2] */
*(++p) /* same as a[1], but p now points to a[1] */- ATrue
- BFalse
99int x, y = 5, *p = &y;
x = *(p++); /* x is set to the contents of the location whose address is in p, and then the pointer stored in p is incremented */
x = *(++p); /* the pointer p is incremented, and then x is equal to the contents of the new location */
x = (*p)++; /* the variable x is set equal to the contents of the location whose address is in p, and then the contents of the location whose address is in p is incremented. The pointer p remains unchanged. *
x = ++(*p); /* the contents of the location whose address is in p is incremented, then x is set equal to the contents of the location whose address is p. The location x will be assigned to the new, incremented value. The pointer p remains unchanged. */- ATrue
- BFalse
100All input and output in C is done in streams
A stream is a sequence of bytes of data- ATrue
- BFalse
101Text streams consists of characters and are organised into lines
Binary streams can handle any data, including characters- ATrue
- BFalse
102A character constant is an integer value represented as a character in single quotes.
For example, 'z' represents the integer value of z (122)- ATrue
- BFalse
103getchar() gets a single character from stdin (standard input)
putchar() puts a single to stdout. (standard output)- ATrue
- BFalse
104A string in C is an array of characters ending in the null character ('\0').
- ATrue
- BFalse
105char color[] = "blue";
char *colorPtr = "blue";
The first definition creates a 5-element array color containing the characters 'b', 'l', 'u', 'e' and '\0'.
The second definition creates pointer variable colorPtr that points to the string "blue" somewhere in memory- ATrue
- BFalse
106Strings are like arrays, because an array is also a pointer to its first element
- ATrue
- BFalse
107We can use a set [ ] as input modifier. Also, we can check the return value!
( scanf("%[^\n]", str)
Format specifier"%[^\n]"tells to the compiler to read the characters until"\n"is not found. On empty input or matching error scanf() returns 0!- ATrue
- BFalse
108char *fgets(char *str, int n, FILE *stream);
The fgets()reads a line of text from an input stream, but allows to specify the specific input stream and the maximum number of characters- ATrue
- BFalse
109#define FLUSH stdin=freopen(NULL,"r",stdin)
flushes I/O buffer- ATrue
- BFalse
110str[strcspn(str, "\n")] = '\0';
Can be used to "remove" new line characters from the string- ATrue
- BFalse
111In C General Purpose Pointer is called as void Pointer
Pointer to void(i.e., void *) can represent any pointer type.
to de-reference void pointer we need to use *((variable type*)ptr)- ATrue
- BFalse
112Pointer to a constant: const int *ptr;
Constant pointer to variable: int *const ptr;
Constant pointer to constant: const int *const ptr;- ATrue
- BFalse
113Memory

- Ayes
- B-
114Referring to previous picture:
text: instructions that the program runs.
Data: initialized global and static variables.
Bss: uninitialized global and static variables. They are initialized to zeroes by kernel.
Stack:stores local variables and return addresses. it grows downwards (from high to low address).
Heap:memory returned when calling malloc/realloc. It grows upwards (from low to high address)- ATrue
- BFalse
115malloc stands for "memory allocate"
calloc stands for "contiguous allocation"
void *malloc(size_tblock_size); - Allocates a single block of block_size bytes.
void *calloc(size_tnum_elements, size_telement_size);
Allocates multiple contiguous blocks (num_elements) of memory, each block of the same element_size bytes- ATrue
- BFalse
116to free a pointer:
free(ptr);- ATrue
- BFalse
117intx = 3000;
int *ptr;
int **pptr;
/* take the address of var*/
ptr = &x;
/* take the address of ptr using address of operator & */
pptr = &ptr;
Pointer ptr holds the address of x (3000)
pointer pptr holds the address of pointer ptr.- ATrue
- BFalse
118Why do we need pointer to a pointer?
When we use "pass by pointer" to pass a pointer to a function, only a copy of the pointer is passed to the function.
The problem comes when you try to repoint the pointer someplace else inside the function!- ATrue
- BFalse
119We can declare an array of pointers also like:
char *people[]={"Ed", "Mike", "Selena"};
We can now use people[0] anywhere we could use a string- ATrue
- BFalse
120Garbage is a block of heap memory that cannot be accessed by the program
Garbage Collection (GC) -automatic management of dynamically allocated storage- ATrue
- BFalse
121Pointers make automatic garbage collection impossible
- ATrue
- BFalse
122Potential problems with Stack:
Run out of stack space
Unintentionally change values on the stack
Access memory even after frame is deallocated- ATrue
- BFalse
123Potential problems with Heap:
Run out of heap space
Unintentionally change other heap data
Access memory after free'd
free memory twice- ATrue
- BFalse
Information
- Description
- Computer Science Quiz on programming languages midterm, created by Duncan Levings on 26/10/2018.
- Author
- Duncan Levings
- Language
- English
- Views
- 16
- Tags
- Rating
- No ratings yet Rate this quiz