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

Java

java 1 notes

Uploaded by

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

Java

java 1 notes

Uploaded by

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

For theory part

Basics of java

Intro for programming:

A computer is made up of hardware and software:

Hardware – the physical, tangible pieces that support the computing effort

Software – consists of programs and the data those programs use

Program – a series of instructions that the hardware executes one after another

Software programing implementation:

Coding: writing the program in a specific programming language

Compiling: translating the program into a form that the computer can execute

Debugging: investigating and fixing various types of errors that can occur

Syntax: programming language that employs set of rules that dictate how the words and symbols can be
put together

Paradigms: several different styles of writing computer programs

Bytecode: it is the machine code for an imaginary machine called the Java Virtual Machine or JVM (part
of the JRE-Java Runtime Environment)

Inline documentation: comments in a program, explain the purpose of the program

Note: Software tools can be used to help with all parts of this process including SDKs, and IDEs

Language levels:

1. Machine language
2. Assembly language ( low level language)
3. High – level language ( c, c++, java)
4. Fourth generation languages

CPU is the brains of a computer – it fetches an instruction, tries to makes sense of it, then executes it.
Some of paradigms:

1. procedural (or imperative) - COBOL


2. Functional – Lisp (Artificial Intelligence)
3. Logic – Prolog (Expert systems – Medical diagnosis)
4. object-oriented
5. event-driven
note: java uses mixture of procedural, object oriented and event driven paradigms.
the basic building block in java (objects, and classes)

steps of execution in programming languages:


1. CPU executes the code
2. Program must be translated to machine code before execution
3. Compiler translates the written code into target language

Execution of java programs:

1. The user will tell the machine to compile the source code
2. Java will use the bytecode (half cooked code)
3. Java is not tied to any particular machine
4. , it is the machine code for an imaginary machine called the Java Virtual Machine or JVM
(part of the JRE-Java Runtime Environment)
5. The transformation called just in time (JIT) compilation

Types of errors:

Syntax error: Syntax errors are mistakes in using the language. Examples of syntax errors are missing a
comma or a quotation mark, or misspelling a word.

Run-time error: an error that occurs while the program is running after being successfully compiled.

Logic (semantic) error error is text which is grammatically correct but doesn't make any sense.

Note: java set dose not have a set of IDE

Jjava programing componenets:

1. Classes
2. Methods
3. Statements

Note: a java application always contains a method called main


Note: java code is case sensitive
(unit1) Summary quiz questions:

The process of translating a program into a form that the computer can execute is ________.

Compiling

Translating the program into a form that the computer can execute is called _________

Compiling

Java is case sensitive.

True

Each type of Central Processing Unit (CPU) has its own specific

machine language

class
A program is made up of a Blank 1 Question 5 which includes one or more Blank 2
bytecode statements
Question 5 that contain programming .

A ______ is a software tool which translates source code into a specific target language

Compiler

Investigating and fixing various types of errors that can occur is called ___________

Debugging

The set of rules followed by programming languages to create valid programs is called
Answer: syntax
What commenting style would you use for the following comments?

Answer 1 Question 9
This method greets the user with their name
//

This method prompts the user with the following menu:


1. Addition
2. Subtraction
Answer 2 Question 9
3. Multiplication
/*
4. Division
The user is then prompted for two numbers and the appropriate
result is printed.
The HelloWorld program implements an application that
simply displays "Hello World!" to the standard output. Answer 3 Question 9
@author Maleeha Muzafar
/**
@version 1.0
@since 2023-03-18

Writing the program in a specific programming language is called ____________?

coding
Unit 2

Algorithm: An algorithm is a well-defined sequence of unambiguous instructions. It may content English


text and mathematical expressions

It decribes how a computer or a human may do a task, it cannot be excuted by a computer, its for human
read, must be coded to make program, computers can only excute programs.

Components of algorithms:

1. Instruction (also known as primitive): it must be simple and the system must be capable of
performing.
2. Sequence (of instructions): a series of instructions to be carried out
3. Selection: used to decide which instructions to execute
4. Repetition

Representing values in program:

Literals: exact values typed into the source code of a program

Common types::

1. String literals: often used with printl statments


2. Numbers

Variables: are containers for values, a specific type of values can hold only one value at a time

Note: a variable must be decleard by specifying the variables identifier and the type of information

Constant: A constant is an identifier that is similar to a variable except that it holds the same value
during its entire existence.

Note: constants are important for three reasons:

- They give meanging to unclear literal values (magic numbers)


- They facilitate program maintenance
- It helps to avoid inadvertent errors by (other) programmers

Assignments: an assignment statement changes the values of a variable


Primitive data types:

- Byte,short,int,long (integers 19,-174)


- Float, double (floating point numbers 17.95, -102.33335)
- Char (characters a,b,%,+), A char variable stores a single character
- Boolean (values are true, false), a Boolean value represents a true or false condition
he reserved words true and false are the only valid values for a boolean type
Example: boolean done = false;
A boolean variable can also be used to represent any two states, such as a light bulb being on or
off.

type storage Min value Max value


Byte 8 bits -128 127
Short 16 bits -32,768 32,767
Int 32 bits -2,147,483,648 2,147,483,647
Long 64 bits < -9 x 10^18 < -9 x 10^18
Float 32 bits +/- 3.4 x 1038 with 7
significant digits
double 64 bits +/- 1.7 x 10308 with 15
significant digit

Note: to add a float value u should add an “f” to the value

For example: “float salary = 1221.45f”

Identifiers: special identifiers called reserved words that already have a predefined meaning in the
language, a reserved word cannot be used in any other way.

White space: Spaces, blank lines, and tabs are called white space, is used to separate words and symbols
in a program, Extra white space is ignored.
Expressions:

An expression is a combination of one or more operators and operands, Arithmetic expressions compute
numeric results and make use of the following arithmetic operators:

Examples:

- Addition +
- Subtraction –
- Multiplication *
- Division /
- Remainder %

Note: If either or both operands used by an arithmetic operator are floating point, then the result is a
floating point

Division and remainder:

If both operands to the division operator (/) are integers, the result is an integer (the fractional part is
discarded)

Ex:

14/3 = 4

8/12= 0 “just the answer without the remainder”

The remainder operator (%) returns the remainder after dividing the second operand into the first
Operator precedence: operators are combines into complex expressions

The order:

- Operators in parentheses (innermost first)


- multiplication, division, and remainder
- addition, subtraction, and string concatenation
- operators with the same precedence are evaluated from left to right
- assignment operator

note: assignment operator has the lower precedence that the arithmetic operates

the right and left hand sides of an assignment statement can contain the same variable.

Increment and decrement:

- The increment and decrement operators use only one operand.


- The increment operator (++) adds one to its operand
- The decrement operator (--) subtracts one from its operand
- The statement count++; is functionally equivalent to count = count + 1
Data conversion:

- Widening conversions are safest because they tend to go from a small data type to a larger one
(such as a short to an int)
- Narrowing conversions can lose information because they tend to go from a large data type to a
smaller one
Three ways to occur data conversions:
- Assignmet conversion
- Promotion
- Casting

Assignment conversion:

Assignment conversion occurs when a value of one type is assigned to a variable of another

.E.g. the following assignment converts the value in dollars to a float.

float money;

int dollars = 3;

money = dollars;

Note: the value or type of dollars did not change, Only widening conversions can happen via assignment

Promotion:

Promotion happens automatically when operators in expressions convert their operands

Ex: results = sum / count;

Note: if sum is a float and count is an int, the value of count is converted to a floating point value to
perform the calculation

Casting: helpful to treat a value temporarily as another type.


Summery quiz:

Which one of the following is not true about Assignment Statements?

b.The assignment operator is a plus sign '+'

A variable can hold multiple values at any time.

False

Select the right data type for each of the given values:

true boolean
Answer 1 Question 3
1.5 double
Answer 2 Question 3
'F' char
Answer 3 Question 3
500 int
Answer 4 Question 3
96.5f float
Answer 5 Question 3

An algorithm must not contain English text or mathematical expressions

False

What is the difference between a constant and a variable?

c. A constant's value remains unchanged during its entire existence

Select the right conversion type for each of the provided definitions:

Answer 1 Question 6
...treat a value temporarily as another type. Casting

...occurs when a value of one type is assigned to a Answer 2 Question 6


variable of another. Assignment Conversion

...happens automatically when operators in expressions Answer 3 Question 6


convert their operands. Promotion
Look at the code below:

int salary = 400;


salary++;

What is the value of the variable "salary"?


c. 401

When do we normally design algorithms?

Before writing our code

Which one of the following is not a component of an algorithm?


e. Compile

Look at the expression below:


int answer = 10 + 5 * 2 - (5 + 3);

What would be the value stored in "answer"?

b.12

In the variable declaration below:


int total;
What is "int" ?

c.The variable's data type.

double score;
int points = 10;
score = points;
The code above is an example of what type of conversion?
a.A widening conversion
Which of the following choices is NOT a programming convention?

d.Use all lower_case for constant identifiers


Spot on. When declaring constants, their identifiers should be in all UPPER_CASE (example:
MAX_VALUE)
Unit 3

Object oriented programming: Java is object oriented program language, object is fundamental entity in
java program.

Why?

- It helps you break up your program into all its different components
- You begin to think about what data you will have in your program and what things you can do to
that data
- It helps you to reuse your code by creating “templates” for things

Object: it has state and behavirs.

State: descriptive characterstics , behaviors: what it can do and what it can be done

Note: The state of a bank account includes its account number and its current balance The behaviors
associated with a bank account include the ability to make deposits and withdrawals, the behavior of an
object might change its state

Classes: An object is defined by a class

A class is like a template or blueprint that describes the object. It contains:

• Attributes: which give the state of an object

• Methods: which give the behaviours of an object

A class represents a concept, and an object represents the embodiment of that concept

An object is said to be an instance of a class, We can create multiple objects from the same class, the
class that contains the main method of a Java program represents the entire program

Creating objects:

- A variable holds either a primitive type or a reference to an object


- A class name can be used as a data type to declare an object reference variable, e.g. Car aCar;
- No object is created with this declaration
- An object reference variable holds the address of an object
- The object itself must be created separately
- Use “new” operator to creat an object
- Creating objects called instantiation
- An object is an instance of a particular class
Invoking methods: use the dot operator to invoke an objects behaviour.

- A method may return a value, which can be used in an assignment or expression


- A method invocation can be thought of as asking an object to perform a service

Packages: The Java standard class library is organized into packages containing ready-made classes.

The import declaration:

• When you want to use a class from a package, you could use its fully qualified name

java.util.Scanner

• Or you can import the class, and then use just the class name

import java.util.Scanner;

• To import all classes in a particular package, you can use the * wildcard character

import java.util.*;

• All classes of the java.lang package are imported automatically into all programs

• It's as if all programs contain the following line

import java.lang.*;

• That's why we didn't have to import the System or String classes explicitly in earlier programs

• The Scanner class, on the other hand, is part of the java.util package, and therefore must be
imported

Basic input:

• The Scanner class which is part of the standard Java class library, provides methods that we can
use to read input from the keyboard

• As a general guideline, it requires the following:

• import java.util.Scanner;”

• or “import java.util.*;”

(NOTE the “*” indicates that you are importing all the classes from the Java util package)

• we must create a new Scanner object

• new values can then be read using the Scanner object

• In order to use the Scanner functionality, we need to explicitly specify the classes that will be
used in our program
• We do this by including an import statement at the very top of our code, above our class

import java.util.*;

public class Welcome

...

• We now have the ability to use the Scanner class but first we have to declare a new Scanner
object to use:

import java.util.*;

public class Welcome

Scanner scan = new Scanner(System.in);

• Note in this case, the Scanner object is called “scan”

• Now that we have a Scanner object we can read data in

• We have to specify what type of data we are going to read in, using the following commands:

• scan.next(); … for reading strings of characters

• scan.nextInt(); … for reading integers

• scan.nextDouble(); … for doubles

• scan.next().charAt(0); … for characters

• scan.nextLine(); … for all input on a line

• Each of these commands will read the next piece of data up until a space, tab, or newline. These
characters that separate elements of the input on both sides are called delimiters.
The string class:

• int, char, double, boolean etc. are examples of primitive data types

• These are built-in to Java and are the basis of other types.

• A String is a class, not a primitive data type.

• A String is a collection of characters (e.g. "Sally")

• We can declare a String object as:

String name = new String(“Jack”);

• However, as an exception, for the String class, we do not need to use the new operator to create
a String object.

• For example:

String name = "Sally";

• Each string literal - delimited by double quotation marks - represents a String object.

• A String value is always written in double quotes.

• You can have an empty String, shown as "".

String emptyString = "";

• A simple example is string concatenation:

String title = "When Harry met " + name;

• Some methods may refer to the index position within a String.

• The index position specifies a particular position within a String and starts from the first
character in the String, beginning with 0.

• E.g. for a String “Jack Sparrow”, the character ‘S’ is at index 5, the character ‘w’ is at index 11

String methods:

• Some common String methods include:

name.length()

returns the number of characters within a string

name.charAt(5)

returns the character at the specified index

name.toUpperCase()

returns a new String identical to this string except all


lowercase characters are converted to its uppercase equivalent

name.toLowerCase()

returns a new String identical to this string except all uppercase characters are
converted to its lowercase equivalent

name.subString(2)

returns a new String that is a subset of name starting at index 2 and extending

to the end of the string name

name.indexOf(“a”)

returns the index of the specified character

note: a string object is immutable, the value cannot be lengthed or shortend.

Using scanner to read separated strings:

Sometimes we might need to break down a long string entered by a user into

its separated strings, e.g. the words in a sentence. We can do this by creating

another Scanner object from the input string and calling the useDelimiter

method of the second Scanner. The next method of the second Scanner

returns each String which is separated by the Delimiter, which can be a space

or other character.

In this example, if a user enters a name which is made up of 3 names, e.g. Ali

Mohamed Abdulla, the output will show each name separately:

Scanner in = new Scanner(System.in);

System.out.println("Enter Name");

String temp = in.nextLine();

Scanner name = new Scanner(temp).useDelimiter(" ");

System.out.println("First name: " + name.next());

System.out.println("Second name: " + name.next());

System.out.println("Last name: " + name.next());


Math class:

• The Math class provides a number of mathematical functions that can easily be used

• There is no need to import any package to use the Math class as it is available by default

• All the methods in the Math class are static methods (which will be covered in a later lecture),
which means they can be invoked without having to instantiate an object

• Examples of some methods in the Math class:

double x = Math.sqrt(25);

returns the square root of a number

double p = Math.pow(2,3);

returns the value of the first parameter(2) raised to the specified power of the second

parameter (3) (8.0 in this example)

double rand = Math.random();

returns a random number between 0.0 (inclusive) and 1.0 (exclusive)

double pi = Math.PI;

Returns the value of  (the ratio between the circumference of a circle and its diameter)

int abs = Math.abs(-100);

returns the absolute value of a whole number (100 in this example)

using the decimal format class:

• The Java standard class library contains classes that provide formatting capabilities to allow us to
format output according to certain requirements

• The DecimalFormat class allows you to format values based on a pattern. It is commonly used to
specify the number of decimal places in a floating point result

• The DecimalFormat class is part of the java.text package, so an import is required:

import java.text.DecimalFormat;

• The number of decimal places required is specified in the constructor when a DecimalFormat
object is created (2 decimal places in this example)

DecimalFormat fmt = new DecimalFormat("#.00");

• The format method of the DecimalFormat class is used to apply the format to

the expression:
System.out.println(fmt.format(44.0/3));

Output in this example is: 14.67

Summary quiz – 3:

Which of the following definitions is correct:


a.Objects are instances of classes

Which of the following can be described as Object


Behavior:
a.Add money to a bank account

The class definition contains the following components:


c.Attributes

How many objects can be created as instance of a single


class?
Multiple

Which of the following classes could be considered as


a derived class of the class "Celestial body":
Commet
Satellite of the planet
Planet
All of listed
What is the name of the special method that is executed automatically
when creating new object:
Constructor
Which operator is used to invoke a method for an object:
Dot operator (".")
Which keyword returns the result of the method execution:
return
What is the name of class containers in Java:
Package
Which of the following is a package from Java standard class library
Java.Util
Which Java-package is imported automatically into every Java-program:
java.lang
What is the name of the standard Java-class that gets an input from the
user:
Scanner
The call to scanner Name.nextInt() is used for reading in:
Decimal Numbers
Which of the following is a class, not a primitive data type:
String
There is no need to import any package to use the Math class as it is
available by default:
True

False
Unit 4A:

Flowcharts:

• Refer to flowchart Tutorial in Lab Manual - Unit 4.

• Important Terms :

• Terminator

• Data Block

• Process (Action)

• Decision (Question)

• The flow of control in a running program refers to the order in which the statements are executed

• The statements within a program are usually executed in a linear fashion unless specified otherwise

• Control Structure : Instructions or statements in a programming language which determine the sequence of execution
of other instructions or statements (the control flow).

• To control the program flow and to make decisions, we use control structures combined with conditional operators
and logical operators.

Controlling program flow:

• We use three control structures:

- Sequence
- Selection (branch)
- Repetition (Loop)

• We use multiple conditional and logical operators:


- Equality (==)
- Inequality (!=)
- Comparison (<, >, <=, >=)
- And (&&)
- Or (||)
- Not (!)

Selection:

- A statement that decides which of two possible sequences is executed


- The decision is based on a single true/false condition

Selection if and if else:

- One way
- Two way
- Multiple selectons- cascaded ifs, nested ifs

The if or one way selection:

- Determines whether to execute some statement(s)


- Decides what to do by evaluating a Boolean expression
- If the expression is true, execute the block
Blocks:

• a single statement (ending with a semicolon)

if (x % 2 == 1)

System.out.println("x is odd");

or

• a series of statements surrounded by braces

if (x % 2 == 1)

System.out.println("x is odd");

• Be careful with

if (x % 2 == 1)

System.out.println("x is odd");

System.out.println("x is a number");

Which is very different to:

if (x % 2 == 1)

System.out.println("x is odd");

System.out.println("x is a number");

In the 2nd example “x is a number” is printed all the time.

Only the 1st statement “x is odd” is part of the if block

Indentation is wrong and confusing to the reader

Two-way selection: the else statement

It oocur after an if statement, one or the other will be executed but not both.

The following piece of code is taken from the previous “IF statement” example. It has been modified to handle the case of
the user not winning the prize

public static void main(String[] args)

Scanner input = new Scanner(System.in);


int number;

System.out.println("Please enter a number between 1 and 10 :");

number = input.nextInt();

if (number == 7)

System.out.println("You win todays grand prize!");

else

System.out.println("Better luck next time!");

Using braces in if statements:

Using braces and aligning braces are important programming conventions

which help to make your code readable and maintainable. In addition omission

of braces may generate a syntax error in some cases. For example the code below would

cause a syntax error if braces were not used in the first branch of the if

statement, because there is more than one statement:

if (number == 7)

System.out.println("You win todays grand prize!");

System.out.println(“Well done!");

else

System.out.println("Your guess was not a winning number. “

Omission of braces may also change the logic of your code.

Note: The equality operator == cannot be used to compare Strings.

Instead we use the equals or equalsIgnoreCase methods of

the String class:

String choice = “”;


System.out.println(“Do you wish to end the program?”);

choice = in.next();

if(choice.equalsIgnoreCase(“yes”)

//code to end the program

Comparing Booleans:

When checking the value of a boolean variable in an if statement we can use == true, or

just the variable itself:

Comparing float values:

• Because floating point values are usually stored as an approximation, you should rarely use the equality operator (==)
when making comparison when two floating point values

• The two values will only be considered equal only if their underlying binary representations match exactly

• In many situations, you might consider two floating point numbers to be “close enough” even if they aren't exactly
equal.

• E.g.

If ((f1 – f2) < 0.00001)

{System.out.println("Considered equal");}
• To make decisions we must be able to express conditions and make comparisons

• These expressions are known as boolean or logical expressions and will evaluate to either true or false

• The result of an expression will determine which particular statement will be executed next

• We can express conditions or make comparisons using boolean operators

• Complex boolean expressions are formed with boolean operators

• Relational/Comparison Operators:

• Equality (==)

• Inequality (!=)

• Comparison (<, >, <=, >=)

• Logical Operators:

• And (&&)

• Or (||)

• Not (!)

• Highest to lowest:

• Brackets

• Not (!)

• Comparison (<, >, <=, >=)

• Equality (==) , Inequality (!=)

• And (&&)

• Or (||)

Note: The assignment operator (=) is lower in order of precedence than all Boolean operators.

If condition with multiple expressions:


Nested if:

An if statement may be ‘nested’ or contained inside another if

statement. In some cases the only way to process the logic of a

problem is to use nested if

Syntax:

if(condition 1)

if(condition 2) {

statement

Switch statements:

• The variable evaluated can only be of type char, byte, short, or int

• A switch statement can only be used to determine equality (==) and not to determine relational operator
(less than, greater than, etc)
Unit 4B:

- Module: is any unit which is both small enough and large enough to be useful and self-contained
- Breaking a program into smaller modules is called modularisation
Two levels:
- Methods
- Classes and objects
- Method: is a group of code statements which are given a name, Intended to achieve some specific goal or task in the
program, Can be used in various places in your program, Allows for a form of abstraction

Outlining or planning a method:

- Think of the primary responsibility of the method


- Input

• purpose is to obtain data from user

- Processing

• purpose is to perform manipulation or calculation to give a result

- Output

• purpose is to report information to the user

- Coordination

• Purpose is to coordinate the sequencing of delegating to other methods.

Syntax of a method:

• The first line of the method is the header

• The header describes features about the method. For now, the key thing it specifies is the name of the
method

• The rest of the method (delimited by curly braces) is the body.

• The body is the set of instructions that tell the computer how to do the task that this method is designed to
do.

Methods calls – invocation:

- The mechanism by which to request a method to commence is termed a method call.


- We call or invoke the method
- When a method commences executing, we say it has been called or invoked.
- A method is always called by some other method
- You should not call a method from inside itself
(at least not for this unit)
- The method which causes the call is referred to as the caller or invoker.
- The caller will suspend executing until the called method concludes.
flow of control:

• The order in which statements are performed is called the flow of control

• Flow of control always starts in main()

• Usually proceeds statement by statement

• Decisions can cause statements to be skipped

• Repetition can cause a return to an earlier statement

• Calling a method affects the flow of control

• The caller (who had control) will be paused while the called method is handed control.

• After the called method concludes, it gives control back to the caller method

The caller will continue from where it was up to.

Local variables:

• Each method can declare its own variables

• main() method had 3 variables scan, x and y

• doSomething() method had 1 variable z

• The variables in one method are not accessible by code in any other methods

• These are called local variables

• Variables declared in main() are local variables of main()

• Scope refers to the parts of the code in which an identifier (i.e. variable name) has meaning to the compiler.

• It will be the area from declaration to the end of the method’s code

• Lifetime refers to how long the variable exists in memory, at run time

• When the method finishes, all local variables are deleted from memory.
The void keyword:

Syntax for methods which return data:

in the case of a method which returns data we cannot call the method by itself in isolation from other code. This is because the
method call evaluates to a value when the method is executed. The code below would not display the value returned by the
method:

public static void main()

introduction();

public static String introduction()

String myName;

System.out.println(“Please enter your name”);

myName= scan.next();

return myName;

}
Retuning data:

Sending data to a method:

As well as returning data from a method we can also send data to a method.

To do this, we use parameters…


Parameters: passing values

When a method is called, the values of the parameters in the method call are

copied into the formal parameters in the method header:

public static void main (String[] args)

System.out.println(calc (25, 20));

public static int calc (int num1, int num2)

int sum = num1 + num2;

return sum;

Variables can also be passed as parameters

public static void main (String[] args)

System.out.println(displayAreaOfRectangle(width, height));

public static double displayAreaOfRectangle(double width, double height )

double area;

area = width * height;

return area;

}
Persisting data:

Combining the use of parameters to send data to a method and returning data from a method means we no longer need to rely
on class level variables to persist data. The BMI example can be rewritten in this way:

Using method calls as parameters:


Method overloading:

• Method overloading is the process of giving a single method name multiple definitions

• If a method is overloaded, the method name by itself is no longer sufficient to determine which method is being called

• The signature of each overloaded method must be unique

• The compiler determines which method is being invoked by analysing the parameters

• The println method is overloaded

println (String s)

println (int i)

println (double d) // etc.

• The following lines invoke different versions of the println method

System.out.println ("The total is:");

System.out.println (total);

• Be careful: you can only have one version of each signature

int add(int a, int b)

int add(int a1, int b1) //compile time error

float add(int a, int b) //compile time error

summery quiz unit 4A:

graphical representation of program algorithm is called:


Flowchart
Flowchart can contain:

Decision block

Data block
To control the program flow and to make decisions, we
use control structures combined with conditional
operators and logical operators
True
False

The control structures are:

Repetitions

Selections

Sequences
The if control structure in Java could be:

One-way selection

Multiple selection

Two-way selection
Match operator with its description
Logical OR Answer 1 Question 6
||

Logical AND Answer 2 Question 6 &&

Comparison Answer 3 Question 6


>=

Assignment Answer 4 Question 6


=

Equality Answer 5 Question 6


==
Which of the following is a boolean operator:

||

&&
Which of the following is the correct syntax for checking
the value of a boolean variable isStudent
if(isStudent)
Order the operators by precedence from lowest to highest
1. Answer 1 Question 9 Assignment

2. Answer 2 Question 9 Logical OR (||)

3. Answer 3 Question 9 Logical AND (&&)

4. Answer 4 Question 9 Equality(==)/Inequality (!=)

5. Answer 5 Question 9 Comparision (< >=)

6. Answer 6 Question 9 Not (!)

7. Answer 7 Question 9 Brackets

An if-statement contained within another if-statement is


called
Nested if
Summery quix 4B:
What happens with the caller method, while the control
goes to the called method:
The caller method will be paused, until the called method
finishes working
The first line of the method is called:

Header
A keyword describing a method that does not return a
value is:
Void
What is the output of this program?
public class Test {
public static void main(String[] args) {
int nb = 4;
incrementByTwo(nb);
System.out.println("nb = " + nb);
}
public static void incrementByTwo(int num) {
num = num + 2;
}
}
nb = 4
What is the output of this program?
public class Test {
public static void main(String[] args) {
int nb = 4;
nb = incrementByTwo(nb);
System.out.println("nb = " + nb);
}
public static int incrementByTwo(int num) {
num = num + 2;
return num;
}
}
nb = 6

You might also like