0% found this document useful (0 votes)
2 views3 pages

Solution to Mid Term

The document provides solutions to a midterm exam on programming fundamentals, covering topics such as the differences between compilers and interpreters, the importance of data types, and the functionality of if statements. It also addresses common programming errors, including uninitialized variables and syntax issues, and includes a dry run example for a string input. Additionally, it explains method overloading and the use of ternary operators to determine the largest of three integers.

Uploaded by

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

Solution to Mid Term

The document provides solutions to a midterm exam on programming fundamentals, covering topics such as the differences between compilers and interpreters, the importance of data types, and the functionality of if statements. It also addresses common programming errors, including uninitialized variables and syntax issues, and includes a dry run example for a string input. Additionally, it explains method overloading and the use of ternary operators to determine the largest of three integers.

Uploaded by

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

Sample Solution to Midterm Exam (Programming Fundamentals)

Solution to Question No.1

1. Compiler vs. Interpreter:


o Compiler: A compiler translates the entire source code into machine code (or an intermediate
bytecode) before execution. The resulting executable file can then be run independently.
o Interpreter: An interpreter executes the source code line by line, directly. There's no separate
compilation step.
2. Importance of Data Types:
o Specifying a data type tells the compiler or interpreter how much memory to allocate for the
variable and what kinds of operations can be performed on it. This helps prevent errors and ensures
data integrity. It also allows for optimization of code by knowing the exact data type being used.
3. Fundamental Purpose of an if Statement:
o An if statement (or conditional structure) allows a program to make decisions.

It executes a block of code only if a specified condition is true, enabling branching and control
flow.

4. Differences Between 7, '7', and "7":


o 7: This is an integer literal, representing the numerical value seven.
o '7': This is a character literal, representing the character '7'.
o "7": This is a string literal, representing a sequence of characters (in this case, a string containing
the single character '7').
5. while vs. do-while Loop Condition Checking:
o while loop: The condition is checked before each iteration. If the condition is initially false, the
loop may not execute at all.
o do-while loop: The condition is checked after each iteration. This guarantees that the loop will
execute at least once.
6. Logical Short-Circuit Operators in Java:
o Java's logical short-circuit operators (&& and ||) evaluate expressions from left to right.
o && (logical AND): If the left operand is false, the right operand is not evaluated, as the entire
expression is guaranteed to be false.
o || (logical OR): If the left operand is true, the right operand is not evaluated, as the entire
expression is guaranteed to be true. This optimization can improve performance.
7. Syntax Error vs. Logical Error:
o Syntax Error: A syntax error occurs when the code violates the grammatical rules of the
programming language. It is detected by the compiler or interpreter during the parsing stage and
prevents the program from running.
o Logical Error: A logical error occurs when the code is syntactically correct but produces
unexpected or incorrect results due to flaws in the program's logic. These errors are not caught by
the compiler or interpreter and often require debugging.
8. Primary Advantage of Methods (Functions):
o Modularity and code reusability. Methods allow you to break down a program into smaller,
manageable, and reusable blocks of code, improving readability, maintainability, and reducing
code duplication.
9. Pass-by-Value:
o "Pass-by-value" means that when an argument is passed to a method, a copy of the argument's
value is created and passed to the method. Any changes made to the parameter within the method
do not affect the original argument outside the method.
10. Method Overloading:
Sample Solution to Midterm Exam (Programming Fundamentals)

o "Method overloading" (or function overloading) allows you to define multiple methods with the
same name but different parameter lists (different number, types, or order of parameters). The
compiler or interpreter determines which method to call based on the arguments provided in the
method call.

Solution to Question No.2

Errors Found and Reasons:

1. Error: Using uninitialized variable playerName.


o Location: system.out.println("Player: " + playerName);
o Reason: The local variable playerName is declared (String playerName;) but it is never assigned
a value before being used in the println statement. In Java, local variables must be explicitly
initialized before their value can be read. This will cause a compile-time error.
2. Error: Incorrect casing for System.
o Location: system.out.println("Player: " + playerName);
o Reason: Java is case-sensitive. The standard output stream object is part of the System class, which
must be capitalized. Using system (lowercase) is incorrect. This is a compile-time error (Syntax
Error).
3. Error: Missing semicolon (;).
o Location: At the end of the line System.out.println("Grade: Not A") inside the else block.
o Reason: In Java, statements must typically end with a semicolon. Forgetting it breaks the structure
of the code. This is a compile-time error (Syntax Error).
4. Error: Potential Division by Zero.
o Location: int average = totalScore / numGames;
o Reason: The variable numGames is initialized to 0. Attempting to perform integer division by zero
is an illegal mathematical operation. If this line is executed, it will cause a
java.lang.ArithmeticException: / by zero runtime error, crashing the program. Although
the code might compile, it will fail during execution.

Solution to Question No: 3

Dry run for input 1001

Dry Run:

1. main method starts:


o A Scanner object is created to read input from System.in.
o "Enter a string: " is printed to the console.
o The user enters "1001", and inputData becomes "1001".
o The checkString(inputData) method is called.
2. checkString method starts:
o str is "1001".
o str is not null.
o processedStr becomes "1001" (lowercase, but it doesn't change since it's already lowercase
digits).
o length is 4.
Sample Solution to Midterm Exam (Programming Fundamentals)

o left is 0.
o right is 3 (4 - 1).
3. while loop:
o Iteration 1:
▪ left (0) < right (3) is true.
▪ processedStr.charAt(0) ('1') is compared to processedStr.charAt(3) ('1'). They are
equal.
▪ left becomes 1.
▪ right becomes 2.
o Iteration 2:
▪ left (1) < right (2) is true.
▪ processedStr.charAt(1) ('0') is compared to processedStr.charAt(2) ('0'). They are
equal.
▪ left becomes 2.
▪ right becomes 1.
o The loop condition left (2) < right (1) is now false, and the loop terminates.
4. checkString method returns true.
5. Back in the main method:
o result is true.
o "Input '1001' meets the required property: True" is printed to the console.
o The Scanner is closed.

Output:

Enter a string: 1001


Input '1001' meets the required property: True

Solution to Question No: 4

Input: int a=15, b=12, c=20;


Code: int largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);

1. a > b:
o 15 > 12 is true.
2. (a > c) ? a : c:
o Since a > b is true, we evaluate this part.
o a > c is 15 > 20, which is false.
o Therefore, the result of this inner ternary operator is c, which is 20.
3. largest = 20:
o The variable largest is assigned the value 20.

Therefore, the output of the code is that the variable largest will hold the value 20.

You might also like