Programming Languages - Review for Test
- Explain the difference between machine language and a high
level language.
-- A CPU (microprocessor) can only understand instructions
written in machine language. These are numbers that people cannot
understand. A High Level Language uses commands written in plain text
(similar to English) that people can understand. In machine language,
each single instruction is very simple and does very little, so programs
consist of lots and lots of simple instructions. In HLLs, commands
are more powerful (and understandable for people), so programs can be shorter.
- Explain the difference between assembly language and machine
language.
-- These are very similar. For each machine language
instruction code (numbers) there is a corresponding mnemonic (symbol), so
the translation is quite simple. But people find it much easier to
remember the mnemonics than the machine code numbers.
- If a CPU (microprocessor) can only execute machine code, outline
the process
that enables a Java program to be executed by the CPU.
--
A compiler translates the Java source code into "p-code". Then
the Java Virtual Machine can run and execute the p-code. The JVM must
send appropriate commands to the CPU so it can do what the p-code program
says.
- Explain the brilliant idea that John von Neumann suggested that is still
used
in virtually every modern computer.
-- von Neumann is given credit for
the "stored program" concept. This means that a program
is stored in the memory as numbers (machine code). This is the same
memory that also holds data. The CPU fetches instructions from the
memory and executes them.
- Approximately when were each of these invented:
(a) the first electronic
computer - - 1940
(b) the fist compiler -- around 1960
(c) the first microprocessor
-- 1970
(d)
the first personal computer -- 1980
- Structured High Level Languages belong to the 3rd generation
of
programming languages. Outline what we should expect from
the 4th generation.
-- We expect a natural language interface,
and probably also a natural language syntax for programming languages. This
includes a more fault tolerant attitude from the computer - e.g.
it can still run a program if I forget to type a semi-colon.
- List the following in order from oldest to newest:
Java
Basic C++
Fortran
-- Fortran (1950's) -->
Basic (1960's) --> C++ (1980's/90's) -->
Java (1995 to present)
- Outline two advantages of Object Oriented Program over structured
programming.
-- By combining data and methods
into a single object, OOP improves re-usability. My
program can simply create an object and use it, without worrying about side-effects.
So programs are more robust (reliable). This is not
only true for single programmers - it allows groups of programmers (teams)
to easily share classes (modules), and thus speeds development. All
the currently popular software applications (Windows, MS-Office, etc) could
only be created and refined over 20 years by using OOP techniques.
- Explain 3 differences between a compiler and interpreter.
--
Compilers examine the whole program, whereas interpreters look
at one command at a time.
Compilers find all the syntax errors before
the program runs - interpreters start running the program and then crash
when they find a syntax error.
Compilers produce a machine-code file
which can be run over and over again, whereas interpreters must re-interpret
the program every time it runs. That means a compiled program
can run stand-alone, whereas interpreted programs can only run together
with the interpreter.
- Outline the difference between syntax and semantics, including
specific examples.
-- Syntax is about the way commands
must be written - especially punctuation. For example, leaving out
a semi-colon is a syntax error. Semantics is about the meaning
of commands - for example, print means to print something into the
text-console, whereas output causes a GUI dialog to pop up in the
middle of the screen. Print and output sound similar, but have different
meanings.
- Explain why the Java Virtual Machine is essential for ensuring
portability
of Java programs.
-- Since different platforms
(machines) contain different CPUs (microprocessors) and different operating
systems, they require different machine code to function correctly. Each
machine has its own version of the JVM, which translates a compiled Java
class (p-code) into exactly the correct machine code for the particular
machien. The Java class (p-code) can stay the same, but run correctly
on lots of different machines.
- Explain the difference between a keyword and a variable identifier.
--
A keyword is reserved - this means in can only be
used for the purpose defined in Java.
A variable name can
be used for any purpose the programmer decides. But it is not possible
to redefine the meaning or function of a keyword. For example, for
can only be used to make loops - not for any other purpose.
13. State the output of the following code fragment:
String command = "Add 2 plus 4";
String[] words = commands.split("\\s");
output(words.length); --> 4
output(words[2]); --> "plus"
14. Rewrite this method so that it would respond correctly
to the command: "add 12 and 20"
public void tryAdd(String command)
{
String[] info = command.split();
if (info[0].equals("add") && info[2].equals("and") )
{
int a = Integer.parseInt(info[1]);
int b = Integer.parseInt(info[3]);
int c = a + b;
output(c);
}
}
15. Explain why it makes sense to use an interpreter for JavaScript
rather than a compiler.
-- In the WWW, we want web-pages to work immediately.
We don't want the browser to say, "wait a minute while I compile
this code." It also makes it quite simple to change programs and
pages, which happens often in the WWW. And since web-pages are not "mission
critical", it doesn't matter too much if a syntax error crashes the page,
so compiling is not essential.