0% found this document useful (0 votes)
196 views15 pages

INTERMEDIATE PROGRAMMING. Midterm Quiz.

This document provides examples of Java program errors and debugging codes with explanations. It includes 14 examples of Java program errors identified by error codes, along with the debugging codes used to resolve each error. The examples cover topics like data types, operators, and program syntax.

Uploaded by

Gael Louise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views15 pages

INTERMEDIATE PROGRAMMING. Midterm Quiz.

This document provides examples of Java program errors and debugging codes with explanations. It includes 14 examples of Java program errors identified by error codes, along with the debugging codes used to resolve each error. The examples cover topics like data types, operators, and program syntax.

Uploaded by

Gael Louise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

March 7, 2022

Midterm Quiz
INTERMEDIATE PROGRAMMING

Name: Program:
Student Number: Year level:

Directions:
Part I:
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.
Error codes with explanation
3.1. 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.
Error codes with explanation
3.2. 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.

Error codes with explanation


3.3. 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.
Error codes with explanation
3.4. 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.

Error codes with explanation


3.5. 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.
Error codes with explanation
3.6. Cannot find symbol (4)
Because there is something wrong and missing codes in the line program this
program error arouses because there is something wrong in compiling the java
source code.
Debugging codes with explanation

 String str = "Programiz";


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

Error codes with explanation


3.8. 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.

Error codes with explanation


3.9. Wrong typed of operator (9)
3.12. Incompatible types (9)

This error shows because of there is something wrong in the declaration of


code. 3.9 error pertains to the missing code while the 3.10 error pertains to the
wrong operator encoded in the line program

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 while
the error in the operator we must replace the plus sign (+) into equal sign
(=)

Error codes with explanation)


3.10. 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.
Error codes with explanation
3.11. 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

Error codes with explanation


3.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.

Error codes with explanation


3.14. 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.

Error codes with explanation


3.15. 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 no something 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.
2. Java Ternary Operator
Used to evaluate a variable in either true or false in this program conditional
statement is being use.

Error codes with explanation


2.1. 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.

Error codes with explanation


2.2. 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.

Error codes with explanation


2.3. 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 (;)
Error codes with explanation
2.4. incompatible types: boolean cannot be converted to String (8)
This error shows because there is a value of a certain data type different from
the one expected. Or there is really something wrong in the whole source line
programs that is why Java compiler is unable to resolve a value assigned to a
variable.

Debugging codes with explanation

 Result = (februaryDays == 28)? “Not a leap year”: “Leap year”;


 To resolve this program error. Always make sure that we encode the
correct and complete code.

Error codes with explanation


2.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.

Error codes with explanation


2.6. incompatible types int cannot be converted to Boolean (13)
In this line error instead of checking the relation of equality, the program
cannot be executed because it is not the proper relational operator

Debugging codes with explanation

 Result = (februaryDays == 28)? “Not a leap year”: “Leap year”;


 I changed the plus sign (+) into colon (:) because it serves as the if
expression between not a leap year or leap year to identify if the given
variable is either of the two.
Error codes with explanation
2.7. Variable februaryDays is never read (10)
This happened because again I assigned it right after creating it. Sequential
encoding is very important in programming.
Debugging codes with explanation

 Int februaryDays = 29;


 encoding Is very important in programming.

Error codes with explanation


2.8. 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.

3. Java Assignment Operators 


Used to assign values to a variable. Where the left side operand of the operator is a
variable and in the right-side operand of the operator is a value.

Error codes with explanation


3.1. 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.
Error codes with explanation
3.2. 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.

Error codes with explanation


3.3. 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.

Error codes with explanation


3.4. 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.
4. 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).

Error codes with explanation


4.1. 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.

Error codes with explanation


4.2 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.

Error codes with explanation

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

This error line shows because the declaration of my 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.
Error codes with explanation
4.4. 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.

Error codes with explanation


4.5. 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.

Error codes with explanation


4.6. 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.
Part II:

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 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();

2.Java print()
a. Functions
This method is used to display a text/string inside the quotation
mark on the console.
b. Examples

3.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
4.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
int age = 24; (Variable)
int a = 1; (literals)

5.Java Output
a. Functions
To send output into a standard output and be able to accept the
output data in the program.
b. Examples

6. Java println()
a. Functions
println adds new line after print text on console
b. Examples
7.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);

8.Java String Input


a. Functions
To display a string from the input of the user.

b. Examples
System.out.print("Enter text: ");
String myString = input.next();
System.out.println("Text entered = " + myString);

9.Java Arithmetic Operators


a. Functions
Used to perform arithmetic operations on two variables and data.

b. Examples
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation

10. Java Printing Variables and Literals


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

b. Examples

11.Java Print Concatenated Strings


a. Functions
Merging a variable using the + operator to perform a combination of multiple
strings.
b. Examples

12. 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)

You might also like