0% found this document useful (0 votes)
17 views9 pages

G12 1st Quarter Exam

The document contains a series of programming questions and answers related to Java syntax, conditional statements, and output predictions. It covers topics such as character comparisons, boolean expressions, and the results of various code snippets. Each question is followed by a correct answer, providing a resource for understanding Java programming concepts.

Uploaded by

Helen Avila
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)
17 views9 pages

G12 1st Quarter Exam

The document contains a series of programming questions and answers related to Java syntax, conditional statements, and output predictions. It covers topics such as character comparisons, boolean expressions, and the results of various code snippets. Each question is followed by a correct answer, providing a resource for understanding Java programming concepts.

Uploaded by

Helen Avila
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/ 9

1. If chr is a character variable, which of the following if statements is written correctly?

a. if (chr = "a") System.out.print("true");


b. if (chr == "a") System.out.print("true");
c. if (chr = 'a') System.out.print("true");
d. if (chr == 'a') System.out.print("true");
ANS: D
2. Which of the following is the correct boolean expression to test for: int x being a value
between, but not
including, 500 and 650, or int y not equal to 1000?

a. ((x >= 500 && x <= 650) && (y != 1000))


b. ((x > 500 && x < 650) || !(y.equal(1000)))
c. ((x > 500 && x < 650) || (y != 1000))
d. ((x < 500 && x > 650) || !(y == 1000))
ANS: C
3. What will be the output of the following code?
int num = 10;
if(num < 0) {
num = num + 5;
num++; }
System.out.print(num);

a. 10 b. 11 c. 16 d. none of these
ANS: A
4. What will be the output of the following code?
int num = 10;
if(num < 0)
num = num + 5;
num++;
System.out.print(num);

a. 10 b. 11 c. 16 d. none of these
ANS: B
5. What will be the output of the following code?
int num = - 10;
if(num > 0) {
num = num + 5;
} else {
num = num - 5;
num--; }
System.out.print(num);
a. -10 b. -15 c. -16 d. none of these
ANS: C
6. What will be the output of the following code?
int num = 10;
if(num > 0)
num = num + 5;
else
num = num - 5;
num--;
System.out.print(num);

a. -10 b. -15 c. -16 d. none of these


ANS: D (the output will be 14. Think why.)
7. What will be the value of bonus after the following code is executed?
int bonus, sales = 10000;
if (sales < 5000)
{ bonus = 200; }
else if (sales < 7500)
{ bonus = 500; }
else if (sales < 10000)
{ bonus = 750; }
else if (sales < 20000)
{ bonus = 1000; }
else
{ bonus = 1250; }
a. 200 b. 500 c. 750 d. 1000 e. 1250
ANS: D
8. What would be the value of bonus after the following statements are executed?
int bonus, sales = 1000;
if (sales > 1000)
{ bonus = 100; }
else if (sales > 750)
{ bonus = 50; }
else if (sales > 500)
{ bonus = 25; }
else
{ bonus = 0; }

a. 100 b. 50 c. 25 d. 0
ANS: B
9. What would be the value of bonus after the following statements are executed?
int bonus, sales = 1000;
if (sales > 1000)
{ bonus = 100; }
if (sales > 750)
{ bonus = 50; }
if (sales > 500)
{ bonus = 25; }
else
{ bonus = 0; }

a. 100 b. 500 c. 25 d. 0
ANS: C
10. What is the value of entry after the following statements are executed?
int entry = 9, number = 3;
if ((entry > 9) || (entry/number == 3))
{ entry--; }
else if (entry == 9)
{ entry++; }
else
{ entry = 3; }

a. 3 c. 9
b. 8 d. 10
ANS: B
11. What will be the output of this code?
int grade = 95; String letterGrade = "C";
if (grade < 90)
if (grade >= 80)
letterGrade = "B";
else
letterGrade = "A";
System.out.println(letterGrade);

a. A b. B c. C d. none of these
ANS: C
12. What would be the value of bonus after the following statements are executed?
int bonus, sales = 85000; char dept = 'S';
if (sales > 100000) {
if (dept == 'R')
{
bonus = 2000;
}
else
{
bonus = 1500;
}
} else if (sales > 75000) {
if (dept == 'R')
{
bonus = 1250;
}
else
{
bonus = 1000;
}
} else {
bonus = 0;
}

a. 2000 b. 1500 c. 1250 d. 1000


ANS: D
13. What is the value of the variable named counter after the statements that follow are
executed?
double percent = 0.54;
boolean valid = true;
int counter = 1;
if ((percent > 0.50) && (valid == true)) {
counter += 2;
if (valid == true)
counter++;
else if (percent >= 0.50)
counter += 3;
} else {
counter++;
}

a. 2 c. 4
b. 3 d. 7
ANS: C
14. What is the output of the following code?
int k = 4, j = 10;
if ((k < 7) && (j < 10)) {
if (k + j < 20) {
System.out.println("A grade");
} else {
System.out.println ("B grade");
}
} else {
if (k + j < 20) {
System.out.println ("C grade");
} else {
System.out.println ("D grade");
}
}
a. A grade c. C grade
b. B grade d. D grade

15. public class Test {

public static void main(String[] args){

int temperature = 33;

if(temperature < 0)
System.out.println("Freezing");
else if(temperature < 30)
System.out.println("Pleasant");
else if(temperature < 50)
System.out.println("Hot");
else
System.out.println("Boiling");

}
}
a. Compilation Error
b. Pleasant
c. Hot Boiling
d. Hot

16. public class Test {

public static void main(String[] args){

int i = 51;

if(i > 50)


System.out.println("Greater than 50");
else
System.out.println("Less than 50");
System.out.println("Done");

}
}
a. Greater than 50
b. Less than 50
c. Compilation error
d. Done

17. public class Test {

public static void main(String[] args){

boolean a = true, b = false;

if(a && !b) System.out.println("true");


else System.out.println("false");

}
}
a. True
b. False
c. Compilation error
d. Runtime error

18 . public class Test {

public static void main(String[] args){

int a = 25;
if(a < 25)
System.out.println("1");
if(a > 10)
System.out.println("2");
else
System.out.println("3");
}
}
a. 1
b. 2
c. 3
d. Compilation error

19. public class Test {

public static void main(String[] args){

int a = 23, b = 30;

if(a > 20 && b > 25)


System.out.println("1");
if(a > 30 && b < 35)
System.out.println("2");
else
System.out.println("3");

}
}
a. 1
b. 2
c. 3
d. None of the above

20. Which of the following statement is correct?


a. There must be at least one else statement after else if
statement
b. There must be at least one else statement after if
statement
c. else statement must have corresponding if statement
d. Curly brackets are mandatory for if, else if and else
statements
21. public class Test {

public static void main(String[] args){

int a = 2 * 22 + 13 - 21;
int b = 15 * 3 + 17 - 12;

if(a > b);


System.out.println("Greater");
else
System.out.println("Less");
}
}
a. Greater
b. Less
c. Compilation error
d. Runtime error

22. How many choices are possible when using a single if-else statement?\

a. 1
b. 2
c. 3
d. 4

23. What does the following code fragment write to the monitor?
int sum = 14;
if ( sum < 20 )
System.out.print("Under ");
else
System.out.print("Over ");
System.out.println("the limit.");

a. Over
b. Under
c. Under the limit
d. Over the limit

24. A sequence of statements contained within a pair of braces ("{" and "}") is called
a:
a. block
b. blob
c. branch
d. brick

25. Evaluate (to true or false) each of the following expressions:


14 <= 14 14 < 14 -9 > -25 -25 > -9
a. true true true true
b. true false false false
c. true false true true
d. true false true false

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy