CS 320
1) Print the following "Politically Correct Holiday Tree" to STDOUT: x xxx xxxxx x
#!/bin/bash echo ' x' echo ' xxx' echo ' xxxxx' echo ' x'
Fibonacci - in C
#include<stdio.h> int Fibonacci(int); main() { int n, i = 0, c; scanf("%d",&n); printf("Fibonacci series\n"); for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); }
When choosing a language for a project, what factors should you consider before you start implementation?
1. Which language was designed to solve a particular problem that the project also proposes. 2. How fast the project needs to be finished and how efficient it needs to be
function calls
A function call is an expression that passes control and arguments (if any) to a function and has the form: expression (expression-listopt) where expression is a function name or evaluates to a function address and expression-list is a list of expressions (separated by commas).
loosely typed
A loosely typed language is a programming language that does not require a variable to be defined
parser
A parser is a software component that takes input data (frequently text) and builds a data structure - often some kind of parse tree, abstract syntax tree or other hierarchical structure - giving a structural representation of the input, checking for correct syntax in the process
Struct
A struct in the C programming language (and many derivatives) is a complex data type declaration that defines a physically grouped list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer, or the struct declared name which returns the same address.
Why do most languages prevent you from accessing variables declared in other functions?
A variable's value is typically placed on the stack, and when a function finishes the stack changes resulting in the value at the memory location no longer being saved for that variable (which means it may be used for something else)
In C/C++, why is it not appropriate to return a reference to a local variable?
After the function returns the stacks memory may be overwritten.
Describe the purpose of pointers.
Allows for data to be referenced (shared) amongst a variety of things (functions) that require access. Allows for large sets of data to be referenced without explicitly naming each one.
Which of the following statements is true about an Object?
An Object is a collection of data specific to that object.
Give at least one way that text based interfaces to an OS (such as a Shell) benefit the end user.
Automation is simpler.
bash
Bourne-again shell The Bash command syntax is a superset of the Bourne shell command syntax. Bash is the shell, or command language interpreter, for the GNU operating system.
In C, why did the designers choose to not implement the data type string?
C does not support OOP.
C
C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. Therefore, C was useful for many applications that had formerly been coded in assembly language, for example in system programming. Despite its low-level capabilities, the language was designed to encourage cross-platform programming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with few changes to its source code. The language has become available on a very wide range of platforms, from embedded microcontrollers to supercomputers.
How is C different than C++?
C requires thinking primarily in terms of actions, whereas C++ allows us to think in terms of things.
What would cause a program we wrote in Java to run slower than a program we wrote in C/C++?
C/C++ code can break abstractions, requiring less code to do certain tasks at a low level. We aren't necessarily very good programmers, and our implementations have flaws. Java requires a virtual machine that would have to interpret the java byte code.
class based OOP language
Class-based programming, or more commonly class-orientation, is a style of object-oriented programming (OOP) in which inheritance is achieved by defining classes of objects, as opposed to the objects themselves (compare prototype-based programming).
How do we create new data representations of the ones and zeros in Object Oriented Programming languages?
Define a class. In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).
Defining a class in an object oriented language does what?
Defines a novel data type that the compiler can now work with.
Why do multiple programming languages exist?
Different languages express different ideas well resulting in languages suited for different tasks.
Why do we use malloc and new?
Dynamically allocate memory on the heap.
Pointers are the same in C & C++. True / False
FALSE In C pointer variable can point to a constant variable but in C++ it is compilation error. It is undefined behavior to change value of const variable in C
All programming languages share the same fundamental types.
False
Shell scripting languages only run on Unix/Linux/BSD machines.
False
2) Find and delete all of the files, in the current directory only, that contain the string "#include" in them. Print nothing.
Files=`grep "#include" *` for f in $Files do rm $f done
In C, why did the designers choose to require a format string as an argument to scanf?
Format strings tell the function how to attempt to interpret the data provide.
scope
In computer programming, the scope of a name binding - an association of a name to an entity, such as a variable - is the part of a computer program where the binding is valid: where the name can be used to refer to the entity. A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language − 1. Inside a function or a block which is called local variables. 2. Outside of all functions which is called global variables. 3. In the definition of function parameters which are called formal parameters. Static scoping allows the programmer to reason about object references such as parameters, variables, constants, types, functions, etc. as simple name substitutions. This makes it much easier to make modular code and reason about it, since the local naming structure can be understood in isolation. In contrast, dynamic scope forces the programmer to anticipate all possible dynamic contexts in which the module's code may be invoked.
Why are shell scripting languages usually not appropriate for large scale applications? (One good reason is enough)
Interpreter does not provide very good debugging information.
In what way can we say Java is "Faster" than C (Not referring to runtime)?
It allows for OOP so multiple people can work on a project at the same time.
Which of the following is MOST important when selecting a language for a project/application?
It depends. There is no single correct answer above.
Why would an organization choose to write an application in Java?
It is capable of running on multiple platforms without rewriting the code. It is capable of implementing very large software projects because of its design features. It has a large number of libraries built in that simplify many tasks.
Why would we write a program in Java that would run faster than a program written in C/C++?
Java contains many packages that may implement an optimized version of an algorithm. We aren't necessarily very good programmers, and our implementations have flaws. Java has a restricted syntax/usage preventing us from using less than ideal implementations.
Why is C typically considered to be a "faster" language than Java?
Java typically requires a virtual machine to execute its byte code.
Describe the pertinent differences between the C++ new and the C malloc.
New is more typesafe(but not completely) than malloc. New ensures that the data is initialized by the constructor.
Given a text processing problem should we always use a shell language like bash?
No, not if you don't know how to solve it in bash, but you know a quick solution in another. Yes, bash handles strings well and will always solve the problem better than you could.
What language features would you look for when developing a large scale project? Also indicate why for each.
Object oriented programming - Allows groups to work effectively and protects implementations from inadvertent misuses.
Where do local variables have their memory placed by compilers (in most langauges)?
On the stack.
What is an advantage to using a loosely (dynamically) typed language?
One doesn't need to know much about data types and therefore there is no conflict when using variables on other variables
What do we use shell scripting languages for?
Organizing files or automating tasks
What is the purpose of shell scripting languages?
Organizing the actions of many smaller purpose driven applications to accomplish larger tasks.
What is the purpose of pointers?
Pointers allow programmers to use data on the stack. Pointers allow different sections of a program to have access to the same memory.
Recursion
Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration)
What is scope?
Scope is the region in which a variables name is valid for use.
Tokenization
Separate the string "Hello,How,Are,You,Today" by commas into an array (or list) so that each element of it stores a different word the process of demarcating and possibly classifying sections of a string of input characters. The resulting tokens are then passed on to some other form of processing. The process can be considered a sub-task of parsing input.
shell
Shell script can take input from user, file and output them on screen. Useful to create our own commands. Save lots of time. To automate some task of day today life. System Administration part can be also automated.
When organizing the actions of several programs it is often useful to write programs using:
Shell scripting
Why would you want to specify a memory allocation on the heap in C/C++?
So that it is available after the function finishes.
What is a benefit of using a strictly typed language?
The data types of variables are known and can't be misused
lexical analysis chain
The first phase of compilation. Takes raw input which is a stream of characters and converts it into a stream of tokens which are logical units each representing one or more characters that belong together
How are large scale projects (like Operating Systems) developed in C without OOP?
The problem is broken up into smaller chunks with purpose driven software written for each chunk.
What are the advantages to using a strictly typed language?
The type of input data to a function can be guaranteed.
When would large scale projects (like Operating Systems) use a scripting language during development?
To automate certain parts of the build/compile system. To automate a test suite to prevent regression errors. (Run a test suite to check for errors)
Why do we define classes in object oriented programming languages?
To create new data types and new abstractions.
Why do we use shell scripting languages:
To organize the execution of several different programs at once. To automate complex or repetitive tasks.
Which part of the lexical analysis chain is responsible for breaking the input strings up into potentially valid chunks? A) Tokenization B) CFG / Parser C) Evaluation / Code Emission D) Linker
Tokenization
When language designers make new programming languages, they typically design the language such that variables have limited scope. Why do they choose to make variables have limited scope?
Two different functions/classes can use the same variable name without conflicts.
When discussing variables and values we often talk about using the correct type. What is a data type?
Types tell the compiler and the CPU how to interpret the ones and zeros in memory.
Why is it important for a programmer to understand scope?
Understand when variables will be available for use / how to access a piece of memory from another scope.
What is a result of the above?
Variables have limited scope.
Why do we typically write Operating Systems in C, instead of an OOP language like C++ or Java? (One good reason is enough. There are a couple of fairly straight forward answers.)
We want to keep as close to the bare metal abstractions as possible. We do not always want to introduce new abstractions to these concepts.
When and why would you use a shell scripting language?
When: If the problem is solvable using a collection of smaller tools. Why: Faster to implement. Reliability of using existing code base.
In C, as a programmer, using char arrays instead of strings has a unique benefit. What is it?
You can break the abstraction of the ascii characters and use them as either numbers or characters. abstraction is a technique for arranging complexity of computer systems
Linker
a computer program that takes one or more object files generated by a compiler and combines them into a single executable file, library file, or another object file.
pointers
a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
Structured programming
a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures, for and while loops—in contrast to using simple tests and jumps such as the go to statement which could lead to "spaghetti code" causing difficulty to both follow and maintain.
C++ and Java are different how?
a. C++ and Java implement different versions of OOP with different meanings for polymorphism and inheritance. b. C++ and Java have radically different fundamental data types. c. C++ and Java do not share the same development paradigms. d. None of the above.
automatic parallelize
converting sequential code into multi-threaded or vectorized (or even both) code in order to utilize multiple processors simultaneously in a shared-memory multiprocessor (SMP) machine. The goal of automatic parallelization is to relieve programmers from the hectic and error-prone manual parallelization process.
Malloc
malloc allocates uninitialized memory. The allocated memory has to be released with free.
New
new initializes the allocated memory by calling the constructor (if it's an object). Memory allocated with new should be released with delete (which in turn calls the destructor). It does not need you to manually specify the size you need and cast it to the appropriate type. Thus, it's more modern and less prone to errors.
In C, as a programmer why must you specify a format string when using printf?
printf is written such that it requires the format string as input.
lexical analysis
the process of converting a sequence of characters (such as in a computer program or web page) into a sequence of tokens (strings with an identified "meaning")
Convert a five character string typed on STDIN to uppercase
tr "a-z" "A-Z"