0% found this document useful (0 votes)
11 views

Programming Fundamentals (Final Revision)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming Fundamentals (Final Revision)

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Programming Fundamentals

Programming is the process of preparing an instructional program for a device.


➔ Simplified definition: getting a computer to complete a specific task without making
mistakes.

➔ Computers only understand machine code (binary).

➔ Programming languages are a middleman for translating code into machine code.
➔ Java & Python ( general purpose).
➔ HTML & Css ( specific purposes).
➔ Low level ( Assembly & C).
➔ High level (Java & Python).
➔ Many different programming languages, each with unique uses.
➔ Choosing a language depends on personal preference and specific tasks.

IDEs (Integrated Development Environments) facilitate code writing.


➔ IDs provide a graphic interface, error checking, auto-filling, and project hierarchy.

Syntax is the set of rules that must be followed when writing code in a specific language.
➔ Syntax varies between programming languages.
➔ Breaking syntax rules results in errors.

Console is a text interface for outputting text from a program.

Print statement is used to output text to the console.


Print statement exists in some form in almost every programming language.

Operators

Arithmetic Relational Logical

Used to construct mathematical Used to compare two quantities Used to form compound conditions
expressions by combining two or more relations
< , > , <= , >= , == , !=
=,+,-,/,% && and
! not equal
Modulus operator (%) provides the // or
remainder of a divisional operation.

Variables store information and can be referenced and manipulated.


➔ Primitive variable types: integers (4), boolean (false or True), floats (6.22), doubles
(4.o), strings (“Hi”), characters (“#”).
➔ Variables can be updated and manipulated throughout a program.

Strings are text in programming languages.


➔ String is anything enclosed by quotation marks.
➔ Strings can be concatenated (combined) in the print statement.

String integer
➔ A string is a sequence of characters, such as letters, ➔ An integer is a whole number without any
numbers, and symbols. decimal or fractional parts.
➔ “4” = String. ➔ 4 = Integer.

If you’re dealing with text, use a string. If you’re dealing with numbers, use an integer.

Naming conventions (capitalizing every word) for variables are important for readability.

Conditional statements
change the path of code depending on certain conditions.

If statement: Elseif statement Else statement Switch statement:

if some condition is true, if preceding if or elif runs instructions if functionally similar to many
carry out instructions; else, statement is bypassed, preceding statement(s) if and else statements
do another thing. run the code segment. are evaluated as false. together.

It checks if a condition is The if-else if (elif) The if-else statement Some languages (e.g.,
true. If it is, the program statement handles multiple allows for two possible Java, C#) have a switch
executes a block of code. conditions. outcomes. statement for multi-way
branching.
Use cases: It checks each condition If the condition is true, one
Checking a single condition sequentially and executes block of code executes; It evaluates an expression
and executing code based the corresponding block of otherwise, a different and selects the appropriate
on its result. code for the first true block executes. case based on its value.
condition encountered.
Performing actions based
on user input.

The ternary operator provides a compact way to express a simple if-else condition.
Arrays store multiple variables containing information that is all related to each other.
➔ Arrays are useful for storing lists of information that can be easily searched through.
➔ Programming languages refer to the first cell as zero, not one.
➔ Array size is final and cannot be increased once defined.
➔ When initializing an array, determine its type and size.
➔ All elements in an array must be the same type.
➔ Multidimensional arrays (arrays inside of arrays) are possible.

ArrayLists (Lists) are a growing array which dynamically changes its size.

Lists can be sorted or unsorted.

Dictionaries are likely an array in that it can store multiple values. However, instead of being
referenced lineary, each value is tied to an identifier that is used to reference it (key).

➔ Each key must be unique.

Index is a fancy way of referring to the elements of an array.

Loops are used to repeat a block of code a certain number of times.


➔ Make sure you set up a condition otherwise an Infinite loop will occur.
➔ Loops can be nested inside of other loops.

Loops

While loop: Do-while loop: For loop: For Each loop:


repeat a block of code run a block of code once, run a block of code a certain used for arrays or lists.
while a certain condition is then repeat while a number of times.
true. condition is true.

A while loop is an A do-while loop is an A for loop is commonly used The for-each loop (also
entry-controlled iterative exit-controlled loop. for iterating over a called the enhanced for
statement. sequence (e.g., numbers, loop) simplifies iterating
It executes the loop body list items, or characters in a over elements in an array
The condition is evaluated at least once, even if the string). or collection.
before each iteration. test condition is false
initially. It determines the number of It automatically handles
If the boolean expression in iterations before entering the the iteration and doesn’t
the while loop never The boolean expression is loop. require an explicit index.
evaluates to false, the loop tested when exiting from
will run indefinitely. the loop.
Loop conditions can be broken using the "break" statement.

Loop conditions can be skipped using the "continue" statement.

Functions are reusable blocks of code.


➔ Functions can take parameters (input) and return values (output)
➔ Functions can be called from anywhere in a program.

Importing libraries is as simple as using an import statment.

➔ You can create your own function.


➔ Function naming conventions follow variable naming conventions.
➔ Each language differentiates how you tell the computer you are about to make a
function.
➔ Java (scope + type + name).
➔ Python ( define(def) + name).
➔ The most important thing about making functions that return variables is that no matter
what path your code takes, it must return a variable.
➔ You cannot return one type of variable if you have already defined the function to return
another type.
Functions can be used to reduce redundancy in code.

Functions can be defined by the user or provided by a programming language.

Error Types

Syntax errors Runtime errors Logic errors


are the easiest to find and fix are harder to find and fix (infinite are the hardest to find and fix
(misspelling) (before running the loop) (after running the code). (results) (after running the code).
code).

Debugging is the process of finding and fixing errors in code.

Debugging tools: print statements, console, debuggers, commenting, breakpoint


debugging and debugging modes.

Debuggers allow you to step through code line by line and examine variables.
Liner search starts at the beginning and checks each data point (worst case) (work better
with unsorted).

Binary search is more efficient than liner search (works better with sorted).

Stack is a data structure which contains all of the takes you instruct your program to
complete.
➔ Stack overflow error: when creating a recursive function without a reachable base
case.
➔ Recursion is useful because it breaks large problems into much simpler pieces to
compute.
Programming Interview Questions And Answers

1. explain what you understand by computer programming?


Programming is the process of preparing an instructional program for a device.
Simplified definition: getting a computer to complete a specific task without making
mistakes.

2. explain the various types of errors that can occur during the execution of a
computer program?
Syntax errors are the easiest to find and fix (misspelling) (before running the code).
Runtime errors are harder to find and fix (infinite loop) (after running the code).
Logic errors are the hardest to find and fix (results) (after running the code).

3. explain an algorithm. What are some of its important features?


An algorithm can be defined as a set of finite steps that when followed helps in
accomplishing a particular task. Important features of an algorithm are clarity, efficiency,
and finiteness.

4. provide a brief explanation on variables.


Variables are used for storing the input of a program as well as the computational results
during program execution. These are actually named memory locations. The value
stored in a variable can change during the program execution.

5. Every programming language has reserved words. What are they? Give some
examples.
Reserved words, also known as keywords, are the words that have predefined meanings
in a particular programming language. These reserved words can’t be used or redefined
for serving other purposes. Following are some examples of reserved words:

➔ C – break, case, char, default, else, float, if, and int


➔ Java – abstract, boolean, catch, class, const, double, enum, finally, implements,
instanceof, throws, transient, and volatile
➔ Python – and, assert, continue, def, del, global, not, lambda, raise, and yield

6. What do you understand by loops? Briefly explain the various types of loops.
A loop is a structure in programming that can repeat a defined set of statements for a set
number of times or until a particular condition is satisfied. There are three important
types of loops:

➔ FOR…NEXT Loop – This is the most effective loop when you know beforehand
the total number of times the loop is to be repeated
➔ WHILE…WEND Loop – It keeps on repeating a particular action until the
concerned condition becomes false. This loop is particularly useful when the total
number of repetitions is unknown.
➔ Nested Loop – When a loop is used inside a loop then it is termed as a nested
loop

7. explain program documentation. Why is it important?


Program documentation is the written description of the algorithm(s), coding method,
design, testing, and proper use of a particular computer program. It is valuable for those
who use the program on a day-to-day basis and also for the programmer(s) who are
meant to correct, modify, and update the computer program.

8. What are constants? Explain their types.


A constant is a programming entity whose value can’t be changed or modified during
program execution. Constants are of two main types:

➔ Numeric constants – Includes integers, single-precision, and double-precision


numbers. For example, 22, 24, -898, 4.5, and 73.45
➔ String constants – Includes a sequence of alphanumeric characters enclosed in
double quotation marks. The maximum length of a string constant is 255
characters. For example, “Shimla,” “I Love You,” and “Orange Is the New Black”

9. explain the operators.


Operators are used for performing certain operations on data in a computer program.
These are represented by symbols. For example, / represent mathematical division while
* represents multiplication. There are 4 main types of operators:

➔ Arithmetic – Used for carrying out mathematical operations


➔ Assignment – Used for storing computational results, strings, and values in
variables
➔ Logical – Used for allowing a computer program to make a decision based on
multiple conditions. In other words, logical operators allow combining simple
conditions to form more complex conditions
➔ Relational – Used for defining or testing some kind of relation between two
entities. These operators evaluate to either true or false and produce a non-zero
value

10. explain arrays?


An array is a programming structure that is a collection of several data values of the
same type. In terms of memory, an array is a group of contiguous memory locations
storing data of the same type.

11. What is a compiler?


A compiler is a computer program that translates written code in one programming
language into another language. Typically, compiler refers to a program that translates
source code pertaining to a high-level programming language to a lower-level
programming language for creating an executable program.
12. What is debugging?
Debugging is the process of finding and fixing errors in code.and Debuggers allow you to
step through code line by line and examine variables.

Debugging tools: print statements, console, debuggers, commenting, breakpoint


debugging and debugging modes.

13. Why do adding comments to code is highly recommended?


Any typical computer program contains hundreds to thousands of LOC. Adding
comments is a way to simplify the experience of examining or finding something within
the code easier for others.

You might also like