Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

March 9, 2022

Midterm Exam (50 points)


INTERMEDIATE PROGRAMMING

Name: Gabriella Louise Arroyo Program: BSCS


Student Number: 2021-0031 Year level: 1st

Directions:
Part I: (1 point each)
1. Refer to Java Program Coding and Debugging Class discussions and
presentation. (Item 1-4).
2. Give the Program title functions and descriptions. (Item 1-4 only)
3. Place the following after each item: (Note: Source line number is located at the
end of each statement.).
3.1. Error codes with explanation
3.2. Debugging codes with explanation

3.3. Program output (image)

1. Java instanceof Operator


It is used to test whether the object is an instance of the specified type.
1.1. as of release 9’_’ is a keyword, and may not be used as an identifier (15)
There is an underscore which is not part of the program. That is why this error
arouse. The error explained it that underscore is only a keyword

Debugging codes with explanation

 System.out.println("obj is an instance of Main: " + result2);


 Replace the plus sign (+) in the underscore (_) to correct the statement.

1.2. Wrong typed of operator (9)


The error pertains to the wrong operator encoded in the line program
Debugging codes with explanation

 result = str instanceof String;


 the error in the operator must be replace the plus sign (+) into equal
sign (=)

1.3. empty statement (10)


We always put semicolon in the end of the line program to declare it as a
complete statement in this part the semicolon was doubled that is why this
error shows.

Debugging codes with explanation


 System.out.println("name is an instance of String: " + result1);
 In every source line program one statement is always required. One at a
time.

1.4. <identifier> expected (2)


This error shows because the code does not comply with the Java syntax rules.
In this source line error there is missing code.

Debugging codes with explanation


 public static void main(String[] args) {
 JVM will not considered to run or have an access in the entry point of
the program if there are missing code. Because it always look for a
specific method signature as an entry point of the program.

1.5. Empty character literal (7)


Because of the apostrophe (‘’) this error shows because it is not included in the
source code of the program.

Debugging codes with explanation


 String name = "JavaProgramiz";
 Removing the apostrophe (‘’) in the last part of the line program is the
solution in the error.

1.6. word result is missing (5)


The program error gives a clue on what is missing in the line program to
complete the statement.

Debugging codes with explanation

 boolean result;
 Encoding result is the solution in this line problem to complete the
statement.
1.7. unclosed string literal (10)
This error shows because we forgot to encode a particular symbol to complete
the statement.
Debugging codes with explanation

 System.out.println("Is str an object of String? " + result);


 When creating a string, when we put a starting quotation mark (“) make
sure put an end to it to considered it as a string. End quotation mark (”)
is the missing code in this line program.

1.8. variable obj must not have been initialized (14)


In this line program line 12 affects line 14 because of the inappropriate
operation or wrong declaration, the program can’t initialize its value.

Debugging codes with explanation


There is nothing wrong in this line program. But then because of wrong
declaration in source line 12 this line got affected. The solution to it is to
correct the declaration in line 12 to solve the error in this source line.

1.9. illegal start of type (9)


This error pertains to the missing code in the line program that is why it
cannot be execute successfully.

Debugging codes with explanation

 result = str instanceof String;


 it is important to encode the complete code for the program to
successfully execute the statement that we are declaring. Because in this
part we are pertaining to see the result if the given variable is considered
as a string or not.
1.10. Misspelled data type (4)
Having only one misspelled letter in a code can create an error in a line
program. In this part the word string was misspelled as strung.

Debugging codes with explanation

 String str = "Programiz";


 Correcting the spelling of String in the line program is the solution to
solve the error.
1.11. Cannot find symbol (4)
This program error arouses because there is something wrong in compiling the
java source code, its either wrong or missing codes.

Debugging codes with explanation

 String str = "Programiz";


 Encoding the right code is the solution to solve this error.

1.12. New instance ignored (12)


In this error it happens because I am making a new configuration but in a
wrong statement as what we see in this line there are two == instead of only
one. The arrangement of the program is improper

Debugging codes with explanation


 Main obj = new Main();
 To make the proper arrangement of the program remove the other equal
sign to declare the statement correctly

1.13. Typing small letter (4)


Not following the program syntax rule will cause an error in a program. In this
error the declaration of the string in line 4 was not capitalized.

Debugging codes with explanation


 String str = "Programiz";
 Keeping in mind that programming is key sensitive is a big help for us to
be conscious if a particular code that we are encoding is correct.

1.14. Incompatible types (9)


This error shows because of there is something wrong in the declaration of
code. It pertains to the missing code
Debugging codes with explanation
 result = str instanceof String;
In the line program error, which is the incompatible types the solution to solve
it is encoding the code instace of to complete the statement.
1.15. Variable declaration not allowed here (7)
It pertains to the error that is not considered as one of the java syntax rules.

Debugging codes with explanation

 //Check if str is an instance of


 It is important to clarify first if we are declaring a statement or just a
comment as a guide in programming. Encoding two slash symbols (//)
pertains to inserting a comment in a source line program.

Program output

2. Java Increment and Decrement Operators


Increment and decrement operator is used to increase and decrease the
variable by one. Increment increases the variable by 1 (++a) while decrement
is to decrease the variable by one (--b).

2.1. not a statement (12,18)


The statement is incomplete that is why the program can’t consider
my declaration as a statement.
Debugging codes with explanation
 result1 = ++a;
result2 = --b;

 Always insert the incremental operator (++) and decrement operator (--)
with the variable for us to successfully declare the things that we want to
do with the int. and to run it successfully.
2.2. flip operand of the binary operator (29)
There is a missing close parenthesis in this source line that is why
this kind of program error shows.

Debugging codes with explanation

 System.out.println("After decrement: " + result2);


 I insert a close parenthesis before the semicolon in the last part
of the statement and the error disappears.

2.3. variable b might not have been initialized (22)


This error line shows because the declaration of the variable didn’t
have initial value.
Debugging codes with explanation
Always initialized the value first by encoding type name = expression
because it is where the initializer combines the declaration statement
and assignment statement into concise statement.

2.4. May split into a declaration and assignment, Variable a is never read (16)
This error shows because the variable a wasn’t declared as string at the line
19.
Debugging codes with explanation

 System.out.println("After increment: " + a);


 The debugging codes to correct this error is to put quotation mark (“”)
to declare the line program as a string.

2.5. reached end of file while parsing (20)


This line error pertains to the missing closing curly braces ( }) because we
cannot end the program and be able to run it successfully if we didn’t end or
close it properly.

Debugging codes with explanation


 }
 Always put curly braces at the last part of the program to end it properly
and be mindful on its purpose if it is for the main group or sub group of
the program.
2.6. Variable result1 is never read (17)
The reason why this error keeps on showing no matter how hard we tried to
retype it. Because we are assigning a particular name right after we encode all
the codes.

Debugging codes with explanation

 int result1, result2


 Always follow the processing of codes in chronological order so that we
won’t encounter this kind of error anymore.

Program output

3. Java Assignment Operators 


3.1. invalid method declaration; return type required (3)
this error shows because there is a missing code. We must know the correct
code to return a void method.

Debugging codes with explanation

 public static void main(String[] args) {

 To be able to return a void method we must retype the complete and


correct code in this line. In every program public static void main is
always used as an entry point of the program.

3.2. Variable var is neither read or written to (7)


This error shows because we named a variable but never use it
anywhere else after assigning it a value. In short it is unused that we
why the program informs us.
Debugging codes with explanation

 var = a;
 After creating a variable, it is important to assigned a data type to its
designated purpose.

3.3. Variable a is never read (6)


this error keeps on showing no matter how hard we tried to retype it.
Because we are assigning a variable right after we encode all the codes.
Or we made a change in declaring something a variable.

Debugging codes with explanation


int a = 4;
Follow the processing of codes in chronological order and observe if there are
changes happened that is not match in the variable that we are
declaring.

3.4. illegal start of expression (11,15,19)


This error shows because the declaration of the statement is incomplete and it
cannot be able to execute successfully

Debugging codes with explanation

 System.out.println("var using =: " + var);


 System.out.println("var using +=: " + var);
 System.out.println("var using *=: " + var);
 Always encode the complete code in declaring a statement to run it
successfully and so that we can’t encounter any kind of error.

Program output
4. Java Ternary Operator
4.1. Illegal start of expression (11)
In this part the error is equal sign that is why there's an error in the declaration

Debugging codes with explanation

 String result;
 I removed the equal sign (=) before the semicolon (;)
4.2. package system does not exist (9)
This error arouses because the package name that we named in the folder is
different on what we declared in the program.

Debugging codes with explanation

 package javaternaryoperator;
 Some programmers don’t include to indicate the package name
in the source code program but it is also important because
when we named a folder it will automatically shows in the
program when we start to encode.

4.3. Missing method body, or declare abstract (2)


In this line error the missing method or declaration is the open curly braces ({ )
the program can’t proceed to the starting point of the program because there is
a missing code.

Debugging codes with explanation

 public static void main(String[] args) {


 Putting curly braces ({}) is very important in starting and ending the
program.

4.4. class, interface, enum, or record expected (11)


This error code arouse because there is a compile time error in the program.
Because as what we see at the errors. There is an end curly braces ({) in the
main group but the open curly braces (}) is missing. In short, the program
doesn’t have proper declaration because we didn’t declare the opening or
starting point of the program but we ended it at the last part

Debugging codes with explanation

 public static void main(String[] args) {


 Putting curly braces ({}) is very important in starting and ending the
program.

4.5. void cannot be dereferenced (14)


I mistyped the period before the semicolon that is why this error shows. The
program mistook my mistyped as accessing or retrieving the value pointed to
by a reference. Base on my understanding in this void means null, where there
is no particular type of data that must be returned in short, the function
doesn’t return a value because it is just a mistake and there is no value
indicated in the line it is just a declaration to print an output

Debugging codes with explanation


 System.out.println (result);
 I removed the period (.) before the semicolon (;) to clear the
misunderstanding in my declaration.

Program output
Part II: (2 points each)

1. Refer to Java Program Coding and Debugging Activity (Item 1-12).


2. Explain the functions and give examples of each item. Place your answers
after each item.

1.Java Get Integer Input From the User


a. Functions
A method where the scanner class will get an integer input from the
user.
b. Examples

2. Java Input
a. Functions
The data that the user give to the program.
b. Examples
// create an object of Scanner
Scanner input = new Scanner(System.in);
// take input from the user
int number = input.nextInt();
3. Java Output
a. Functions
To send output into a standard output and be able to accept the
output data in the program.
b. Examples

4. Java print()
a. Functions
println adds new line after print text on console
b. Examples
5. Java Variables and Literals
a. Functions
Java variables is located in memory that holds data and has a unique
name as its identifier while literals are data’s that is used in
representing fixed values.
b. Examples
VARIABLES

int month = 12;


int MONTH = 12;
System.out.println(month); // prints 12
System.out.println(MONTH); // prints 12

LITERALS

int a = 8;
float b = 6.8;
char c = 'G';

6. Java Get float


a. Functions
Data types used to represent decimal values or floating point literals
b. Examples
System.out.print("Enter float: ");
float myFloat = input.nextFloat();
System.out.println("Float entered = " + myFloat);

7. Java Arithmetic Operators


a. Functions
b. Used to perform arithmetic operations on two variables and data.
c. Examples
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation
8. Java Data Types
a. Functions
A type of language where a computer system interpret its value. And
ensures that data is collected in the preferred format and the value of
each property is as expected.
b. Examples
 Boolean type (true,false)
 Byte Type (-128 – 127/ 8-bit)
 Short Type ( -32768 to 32767/ 16-bit )
 Int Type (-2^31 to 2^31-1 /32-bit)
 Long Type (-2^63 to 2^63-1 64-bit)
 Double Type (64-bit floating-point)
 Float Type (32-bit floating-point.)
 Char Type (16-bit Unicode character)

9. Java println()
a. Functions
println adds new line after print text on console
b. Examples

10. Java Printing Variables and Literals


a. Functions
Its function is to display and print the variables without using
quotation mark.
b. Examples

You might also like