0% found this document useful (0 votes)
15 views4 pages

SOLUTION

The document contains answers to a sample midterm exam, including true/false responses, multiple-choice answers, and code snippets for programming questions. It covers topics such as variable initialization, switch case execution, loops, and class definitions in Java. Additionally, it includes a method for finding pairs of elements in an array that sum to a specified number and an Employee class with methods for setting and getting employee details and calculating salary adjustments.

Uploaded by

cowen89070
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)
15 views4 pages

SOLUTION

The document contains answers to a sample midterm exam, including true/false responses, multiple-choice answers, and code snippets for programming questions. It covers topics such as variable initialization, switch case execution, loops, and class definitions in Java. Additionally, it includes a method for finding pairs of elements in an array that sum to a specified number and an Employee class with methods for setting and getting employee details and calculating salary adjustments.

Uploaded by

cowen89070
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/ 4

Answers of Sample Midterm Exam

Question 1
1. False
2. False
3. True
4.True
5.False

Question 2
1. D
2. C
3. A
4. D
5. D
6. A
7. C
8. C
9. B
10. A

Question 3
This program display the following on the screen:
c=b
c=c
c=d
c is not a, b, c, or d
s0 doesn 't say hello world ...
0, 3, 6, 9, 12, 15, 18, 21, 24, 27,

1
The application Test 1 declares one local variable c of type char initialized with the value
'b', and one local variable s0 of type String initialized to " Hello World ".
The switch case that follows the variable declaration will take as expression the value of variable
c. The value of c is matched with each switch case till a match is found. The value of c is 'b' for
that reason the code that is specified in the case 'b': will be executed. The instruction in this case
and other cases in this switch are not ended by break statement. Therefore, the instructions in the
other cases that are after the matched case (case 'b':) until the end of the switch including default
case will be executed.
s0. toLowerCase (); statement will return a string that has the letters of string that s0 refers
converted to small letters (" hello world "). But this method will not change s0.
Therefore the condition of if statement will be evaluated to false and else branch will be executed.
for statement will create the loop that runs 30 times having a counter i initialized to 0, incrementing
i by one in each step until the condition i<30 is fulfilled. Inside the body for there is one statement
if that will test if i is divisible by 3 or not. If it is divisible it will print the value of i followed by
‘,’
Since i is in a range from 0 to 29 the following numbers will be printed when for statement is
executed:
0, 3, 6, 9, 12, 15, 18, 21, 24, 27,

Question 4
import java.util.Scanner;
public class Test1
{ static void pairs_value(int inputArray[], int inputNumber) {
System.out.println("Pairs of elements and their sum : ");
// Iterate through the elements of the inputArray.
for (int i = 0; i < inputArray.length; i++) {
// Iterate through the elements following the current i.
for (int j = i + 1; j < inputArray.length; j++) {
// Check if the sum of inputArray[i] and inputArray[j] equals the inputNumber.
if (inputArray[i] + inputArray[j] == inputNumber) {
// Print the pair of elements and their sum.

2
System.out.println(inputArray[i] + " + " + inputArray[j] + " = " +
inputNumber);
}
}
}
}
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Give the specific number");
int nr=scan.nextInt();
System.out.println("Give the capacity of array number");
int n=scan.nextInt();
int[] arr=new int[n];
for (int i=0; i<n; i++)
{
System.out.println("Give the elemnt " + (i+1) + " of array ");
arr[i]=scan.nextInt();
}
// Call the pairs_value method with an array and specific number.
pairs_value(arr, nr);
}
}
Question 5
public class Employee {
private String emp_id;
private String emp_name;
private int emp_salary;
public Employee() {
3
this.emp_id= 0;
this.emp_name= “”;
this.emp_salary=0;
}

public Employee(String id, String name, int salary) {


this.emp_id= id;
this.emp_name= name;
this.emp_salary= salary;
}

public void setId(String id) {


emp_id= id;
}
public void setName(String name) {
emp_name= name;
}
public void setSalary(int salary) {
emp_salary= salary;
}
public String getId() {
return emp_id;
}
public String getName() {
return emp_name;
}
public int getSalary() {
return emp_salary;
}

public void calculateSalary() {

if ((emp_salary >=1000) && (emp_salary <=5000))


emp_salary = emp_salary + (emp_salary * 2)/100;
}

public String toString() {


return " Employee [ Id = "+ emp_id +"\n Name = " + emp_name+ "\n Total
Salary = " + emp_salary + “]”;
}

You might also like