You are on page 1of 7

MIDTERM EXAM ODD SEMESTER ACADEMIC YEAR 2022/2023

STUDY PROGRAM OF INFORMATION SYSTEM


FACULTY ENGINERRING AND INFORMATIC
UNIVERSITAS MULTIMEDIA NUSANTARA

Mata Kuliah: Algorithm and Data Structure (IS103)

Nama: Reyhan Resha Sasmita NIM: 00000081166

1. In java, you know about two types of data types, Primitive and Non-Primitive data types.
Could you explain what are the differences between those data types and give some
examples?
Answer : Primitive data types are the most basic data types in the Java language,
primitive data types have been defined by Java and are named with keywords, for
example the keywords int, long, char, etc. While non-primitive data types are data types
that are defined by the programmer themselves and usually contain more than one
value. non-primitive can also be called the Reference data type.
The main differences between primitive and non-primitive data types are: Primitive
types are predefined (defined) in Java. Non-primitive types are created by programmers
and not defined by Java (except for String ).
Primitive data types only have values, no properties or methods. Examples of primitive
data types: number, boolean, null, string, integer, long, double.
Non primitive means that it is not defined by default, it must be filled by itself such as
arrays and classes where there are strings, ints, etc.

Example :

String[] name={"rey","yehan",}, int[] city={padang,serpong}

2.
• Explain how the for statement works with an example!
Answer : FOR is a loop statement that is specific to iterations that knows exactly how
many times the iteration will occur.
Writing Form:

for (init-statement; condition-expression; end-expression){


Pernyataan;}
To create a FOR statement, you need the "for" keyword to begin with, followed by 3
expressions in parentheses. Each expression is separated by a semicolon ( ; ). The three
expressions are used to determine the repetition net, each has a different role. init-statement
is generally the place where a variable is set up and initialized. Because that place is where
it will be evaluated once when the FOR statement is started.condition-expression is an
expression that defines the loop, which is a Boolean number or an operation that returns a
Boolean number. If the expression is 1 (true) then the loop will occur but if it is 0 (false)
then the loop will stop or the for loop will be ignored.end-expression is a place where an
operation to increase or decrease the variable in the init-statement. After the statement in
the body of the for loop is executed, the end-expression will be evaluated.And in the for
loop body there are several statements as commands or actions that the CPU must perform
if the loop occurs. If there are many statements then it is mandatory to enclose them with
a pair of curly brackets, if there is only one statement we can not use a pair of curly brackets.

• Explain the function of break and continue statements in the looping function!
Give an example how to implement those statements!
Answer :
The break command if used in a loop serves to 'force stop' the ongoing looping
process. We have also seen the use of the break command in the SWITCH
structure.Break is usually used after the IF condition, to select 'when' the loop
should stop.
Example
the continue command will stop the loop that is currently happening (1 iteration
only), and then continue the iteration of the next iteration, or it can also be called
to 'skip' 1 loop. Just like the break command, continue is usually used after the IF
condition which is used to select 'when' ' loop must be skipped

Example

3. Explain the difference between the concepts of call by value and call by reference in the
implementation of function calls (methods) along with examples of their implementations!
Answer :
Call by Value means calling a method with a parameter as value. Through this, the
argument value is passed to the parameter.While Call by Reference means calling a
method with a parameter as a reference. Through this, the argument reference is passed to
the parameter.In call by value, the modification done to the parameter passed does not
reflect in the caller's scope while in the call by reference, the modification done to the
parameter passed are persistent and changes are reflected in the caller's scope. Following
is the example of the call by value The following program shows an example of passing a
parameter by value. The values of the arguments remain the same even after the method
invocation.
Example Call by value

public class Tester{


public static void main(String[] args){
int a = 30;
int b = 45;
System.out.println("Before swapping, a = " + a + " and b = " + b);
// Invoke the swap method
swapFunction(a, b);
System.out.println("
**Now, Before and After swapping values will be same here**:");
System.out.println("After swapping, a = " + a + " and b is " + b);
}
public static void swapFunction(int a, int b) {
System.out.println("Before swapping(Inside), a = " + a + " b = " + b);
// Swap n1 with n2
int c = a;
a = b;
b = c;
System.out.println("After swapping(Inside), a = " + a + " b = " + b);
}
}

Output
Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
**Now, Before and After swapping values will be same here**:
After swapping, a = 30 and b is 45

Example Call by Refenrence


Java uses only call by value while passing reference variables as well. It creates a copy of
references and passes them as valuable to the methods. As reference points to the same address of
object, creating a copy of reference is of no harm. But if new object is assigned to reference it will
not be reflected.
public class JavaTester {
public static void main(String[] args) {
IntWrapper a = new IntWrapper(30);
IntWrapper b = new IntWrapper(45);
System.out.println("Before swapping, a = " + a.a + " and b = " + b.a);
// Invoke the swap method
swapFunction(a, b);
System.out.println("
**Now, Before and After swapping values will be different here**:");
System.out.println("After swapping, a = " + a.a + " and b is " + b.a);
}
public static void swapFunction(IntWrapper a, IntWrapper b) {
System.out.println("Before swapping(Inside), a = " + a.a + " b = " + b.a);
// Swap n1 with n2
IntWrapper c = new IntWrapper(a.a);
a.a = b.a;
b.a = c.a;
System.out.println("After swapping(Inside), a = " + a.a + " b = " + b.a);
}
}
class IntWrapper {
public int a;
public IntWrapper(int a){ this.a = a;}
}

Output
Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30
**Now, Before and After swapping values will be different here**:
After swapping, a = 45 and b is

4. Explain the concept of array in detail, why we need to use that, and how we use it in the
Java application!
Answer :
Array is a netode used in programming to perform division of functions. The function of
the array is to divide the program into smaller parts. Also known as subroutines or
subprograms. where the function has a return value when the subprogram is called.
Why we need to use that?
• Simplify and speed up program development. This is because the subprogram will
divide up a long program code so that it will have relatively little code.
• Can be used to reduce repetition of certain codes in a program.
• Can Make the program more modular so that it will be easy to understand and
develop.

how we use it in Java application!


how to use it
for example, you have 5 pieces of data with values 1, 2, 3, 4, and 5 with data type integer
(int). You can just declare each data in 1 variable,
for example, int A = 1; int B = 2; int C = 3; int D = 4; int E=5;
But the declaration becomes ineffective because too many variables are used. You can use
arrays in this case. You can use 1 variable, for example, enter all data into variable A with
an array structure like the following.
int A[5] = {1, 2, 3, 4, 5};
Note that array indexes always start at 0 instead of 1. In declaring an array, you must use
the [ ] (bracket) sign.
To calculate the amount of memory needed for the A array variable above is 5 x 2 bytes (2
bytes is the size for 1 piece of data in integer form). If you want to call a data in an array,
for example the 2nd element, then you can call it by writing the following syntax, X =
A[1]; Array variables must be created before use.
This creation occurs when an array element is assigned a value via assignment or input.
The array is created until the highest indexed element is assigned a value. This is often
done in counting loops from one to several upper bounds, so that the array initially has one
element, then two, then three, and so on as the loop loops over and the index increases.
A run-time error will occur if the program tries to access an array element whose index is
higher than the index of any element previously assigned a value. However, array elements
can be assigned values in any order, with some indexes left out. In this case, the highest
index used still defines the upper bound of the array size, but entries in the array that are
not assigned a value by the program will default to the value 0.

You might also like