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

Republic of the Philippines

BATANGAS STATE UNIVERSITY


Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

FINAL EXAMINATION
in CPE402 Computer Programming 2
First Semester, AY 2020-2021

Name: BELEN, KATHLEEN O. Score :

Section: CE - 2101 Date :

INSTRUCTIONS:

1. Read, analyze and write a java code that best describes the given situation.
2. Files must be uploaded in Google Classroom, make sure to attach your file before clicking
the Submit/Turn-in button.
3.This will take 30% of your final grade and will be graded based on the identified criteria,
see attached file.

GOOD LUCK!

1. By using do while loop, write Java program to prompt the user to choose the correct answer
from a list of answer choices of a question (minimum 10 sets of questions).

The user can choose to continue answering the question or stop answering it. See the examplebelow:
What is the command keyword to exit a loop in Java?
a. int
b. continue
c. break
d. exit
Enter your choice: b
Incorrect!
Again? press y to continue:

PROGRAM / NETBEANS

// Final Examination No. 1


package qanda;
import java.util.Scanner;

public class QandA {


public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
String ans, a,b,c,d,y, res;
// QUESTIONS
String q1 = "1. What is the output of this code?\n" +
"ArrayList<String> list = new\n" +
"ArrayList<String>();\n" +
"list.add(\"A\");\n" +
"list.add(\"B\");\n" +
"list.add(\"C\");\n" +
"System.out.println(list.get(1)); ";
String q2 = "2. In java, how many superclasses can your inherited subclass
have? ";
CPE402: COMPUTER PROGRAMMING2|FINALEXAM
Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

String q3 = "3. How many packages can be contained in a java program? ";
String q4 = "4. In java, an array starts counting from what number? ";
String q5 = "5. It is a name of reserved area allocated in memory. ";
String q6 = "6. Which of the following is NOT the key rule to be followed by
everu identifier? ";
String q7 = "7. It is a machine that performs a variety of tasks according to
specific instruction ";
String q8 = "8. It is the intangible part of a computer and consists of data and
the computer programs. ";
String q9 = "9. What are the programs that people use to get their work done?
";
String q10 = "10. He initiated Java language project in June 1991 for use in one
of his many set-top box projects. ";
a = "a";
b = "b";
c = "c";
d = "d";
y = "y";
//Per number
{
do
{
{
System.out.println(q1);
System.out.println("a. C" + "\nb. A" + "\nc. B" + "\nd. 1");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}

if (!ans.equals(c)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct.");
}
while (!ans.equals(c));

CPE402: COMPUTER PROGRAMMING2|FINALEXAM


Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

{
// number 2
do
{
{
System.out.println("\n" + q2);
System.out.println("a. only one" + "\nb. only two" + "\nc. multiple" + "\nd.
none");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}
if (!ans.equals(a)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to
exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct.");
}
while (!ans.equals(a));
}
{
// number 3
do
{
{
System.out.println("\n" + q3);
System.out.println("a. one" + "\nb. two" + "\nc. as many as you need" + "\nd.
none");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}
if (!ans.equals(c)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to
exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct.");
}
while (!ans.equals(c));
}

CPE402: COMPUTER PROGRAMMING2|FINALEXAM


Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

{
// number 4
do
{
{
System.out.println("\n" + q4);
System.out.println("a. 0" + "\nb. 1" + "\nc. 2" + "\nd. 3");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}
if (!ans.equals(a)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to
exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct.");
}
while (!ans.equals(a));
}
{
// number 5
do
{
{
System.out.println("\n" + q5);
System.out.println("a. Constant" + "\nb. Package" + "\nc. Variable" + "\nd.
Method");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}
if (!ans.equals(c)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to
exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct.");
}
while (!ans.equals(c));

CPE402: COMPUTER PROGRAMMING2|FINALEXAM


}
Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

{
// number 6
do
{
{
System.out.println("\n" + q6);
System.out.println("a. The name must not contain any white spaces. " +
"\nb. The name should not start with special characters." + "\nc. All of the
above" + "\nd. None of the above");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}
if (!ans.equals(d)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to
exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct.");
}
while (!ans.equals(d));
}
{
// number 7
do
{
{
System.out.println("\n" + q7);
System.out.println("a. Projector" + "\nb. Software" + "\nc. Computer" +
"\nd. Keyboard");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}
if (!ans.equals(c)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to
exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct.");
}
while (!ans.equals(c));
CPE402: COMPUTER PROGRAMMING2|FINALEXAM
}
Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

{
// number 8
do
{
{
System.out.println("\n" + q8);
System.out.println("a. Software" + "\nb. Hardware" + "\nc. CPU" + "\nd.
Memory");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}
if (!ans.equals(a)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to
exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct.");
}
while (!ans.equals(a));
}
{
// number 9
do
{
{
System.out.println("\n" + q9);
System.out.println("a. Compilers" + "\nb. Appliction Program" + "\nc.
System Program" + "\nd. All of the above");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}
if (!ans.equals(b)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to
exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct.");
}
while (!ans.equals(b));
} CPE402: COMPUTER PROGRAMMING2|FINALEXAM
Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

{
// number 10
do
{
{
System.out.println("\n" + q10);
System.out.println("a. James Gasling" + "\nb. James Gosling" + "\nc.
Jaymes Golsing" + "\nd. James GLosing");
System.out.print("\nEnter your choice: ");
ans = scanner.next();
}
if (!ans.equals(b)) {
System.out.println("Incorrect"+ "\nAgain? Press 'y' to retake, press 'n' to
exit");
res = scanner.next();
if (!res.equals(y)) {
System.out.println("Thank you!");
System.exit(0);
}
}
else
System.out.println("Correct. \nCONGRATULATIONS!!!");
}
while (!ans.equals(b));
}
scanner.close();
}
}

CPE402: COMPUTER PROGRAMMING2|FINALEXAM


Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

OUTPUT:

CPE402: COMPUTER PROGRAMMING2|FINALEXAM


Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

CPE402: COMPUTER PROGRAMMING2|FINALEXAM


Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

2. Write a Java program to display the speed, in meters per second, kilometers per hour andmilesper hour.
User Input : Distance (in meters) and the time was taken (as three numbers: hours, minutes, seconds).
Note : 1 mile = 1609 meters

Test Data:
Input distance in meters : 2500
Input hour : 5
Input minutes : 56
Input seconds : 23

PROGRAM / NETBEANS

// Final Examination No. 2


package speed;
import java.util.Scanner;

public class Speed {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

float timeSeconds;
float mps,kph, mph;

System.out.print("Input distance in meters: ");


float distance = scanner.nextFloat();

System.out.print("Input hour: ");


float hr = scanner.nextFloat();

System.out.print("Input minutes: ");


float min = scanner.nextFloat();

System.out.print("Input seconds: ");


float sec = scanner.nextFloat();

timeSeconds = (hr*3600) + (min*60) + sec;


mps = distance / timeSeconds;
kph = ( distance/1000.0f ) / ( timeSeconds/3600.0f );
mph = kph / 1.609f;

System.out.println("Your speed in meters/second is "+mps);


System.out.println("Your speed in km/h is "+kph);
System.out.println("Your speed in miles/h is "+mph);

scanner.close();
}
} CPE402: COMPUTER PROGRAMMING2|FINALEXAM
Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

OUTPUT

CPE402: COMPUTER PROGRAMMING2|FINALEXAM


Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

3. Write a JAVA program which asks the user for his login and password. Both must be strings. After 5 wrong
attempts, the user will be rejected.

PROGRAM / NETBEANS

// Final Examination No. 3


package testing;
import java.util.Scanner;

public class Testing {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String user,pass;
System.out.println("...Welcome..." + "\n");
int attempts = 0;
do
{
System.out.println("Enter a user: ");

user = scanner.next();
System.out.println("Enter a password: ");
pass = scanner.next();
if (!user.equals("cpegroup3")
|| !pass.equals("password"))
{
System.out.println("Incorrect User/Password!!!\nTry
again...\n");
}
attempts++;
}

while ((!user.equals("cpegroup3")
|| !pass.equals("password")) && attempts != 5);

if (attempts==5)
System.out.println("Login incorrect.");
else
System.out.println("Login correct.");
scanner.close();
}
}

CPE402: COMPUTER PROGRAMMING2|FINALEXAM


Prepared by: Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

OUTPUT/S

CPE402: COMPUTER PROGRAMMING2|FINALEXAM


Republic of the Philippines
BATANGAS STATE UNIVERSITY
Pablo Borbon Main II, Batangas City
COLLEGE OF ENGINEERING, ARCHITECTURE & FINE ARTS
www.batstate-u.edu.ph Tel. No. (043) 425-0139 loc. 118

GRADING CRITERIA

Prepared by:

Engr. ERWIN S. COLIYAT


Lecturer I, EE/CpE
Date:
Checked and Verified by:

Engr. ELENOR M. REYES


Chairperson, EE/CpE Department
Date:
Approved by:

Dr. JESSIE A. MONTALBO


Dean, CEAFA
Date:

CPE402: COMPUTER PROGRAMMING2|FINALEXAM

You might also like