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

Taller Clase semana 5 – OCA

NAMES: LUIS FERNANDO ENCISO CABALLERO

PEDRO LUIS DURÁN LOSADA

1. What are the categories of primitive variables?


a. Int, float, long, byte.
b. Boolean, character, numeric, double.
c. Char, String, long, float.
d. Boolean, character, numeric.

2. Given:
public class Condicional {
public static void main(String args[]){
int x = 0;
int y = 2;
boolean b = ( x != 0 ) && ( ( y / x ) != 0 );
System.out.println(b);
}
}
What is the output?
a. False
b. True
c. Compilation error
d. Runtime exception

3. Given:
public class JavaCertQType4 {
public static void main(String... cmd) {
String a="56";
// INSERT CODE HERE
int b = 589;
int c = 589;

System.out.println (i * b);
}
}
Which options, when inserted individually at //INSERT CODE HERE will enable
class JavaCertQType4 to output value 32984?
a. int i = Integer.parseInt(a);
b. int i = Integer.parseInt(“a”);
c. int i = a;

4. Given:
public class JavaCertQType3 {

public static void main(String... cmd) {


float a= 38.09f;
double b = 10.1122e2;
float c= 5.2897e2f;
System.out.println(a*b);
System.out.println(a*c);
}
}
What is the output?
a. 20148.467
b. Code outsputs
38517.3699542999320148.467
c. Code outsputs
38517.36995429993
20148.467
d. Compilation error
e. Runtime exception

5. How to assign a letter to a char variable?


a. Only use double quotes.
b. use single quotes or assign a positive integer value to a char instead of a
letter.
c. Only use single quotes.
d. none of the above.
6. Give:
public class JavaCertQType5 {

public static void main(String... cmd) {


char 1a = 86;
char data = 'd';
int data1 = 2;
double data_2 = 3;
boolean diy = true;
double $ ;
$= data1 * data_2;
if (diy ==true)
System.out.println($);

}
}
a. Code will compile successfully only if code on line 3 is commented.
b. Code will output the same result if diy is changed to 1diy.
c. Code will compile sucessfully and execute if variable 1a is changed to 12.
d. Code will compile sucessfully and execute if variable 1a is changed to b.
7. Given:
public class JavaCertQType1 {

public static void main(String... cmd) {

int a=10;
int b=20;
a += b;
b += a;
System.out.println(a);
System.out.println(b);

}
}
Select incorrect options
a. Code will output 30 y 50
b. Code will output 30 y 30
c. Code will output 30 y 10
d. Code will output 10 y 20

8. Given:

public class JavaCertQType1 {

public static void main(String... cmd) {

int a=10;
int b=20;
int c=30, d;
d = ++a + b-- + ++c * --a;
System.out.println(d);

}
}
What is the output?
a. Compilation error
b. Runtime exception
c. Code outputs 300
d. Code outputs 309
e. Code outputs 341

9. Given:

public class JavaCertQType6 {

public static void main(String... cmd) {

int a=10;
int b=20;
int c=30, d;
double f= 50;
d = ++a + b-- + ++c*--a - f;
System.out.println(d);

}
}

Select incorrect options:


a. Runtime exception.
b. Code will output 291.
c. Code will compile successfully.
d. Code will compile successfully only if variable f is changed to int.
e. Compilation error.

10. Give:
public class JavaCertQType1 {

public static void main(String... cmd) {

int a=10;
int b=20, c;
a +=b;
b +=a;
c=b++ + ++a;
System.out.print(a==b);
System.out.print(a>=b);
System.out.print(a<=b);
System.out.print(c!=a);

}
}

What is the output?


a. Falsefalsetruetrue
b. Truetruefalsefalse
c. True
True
False
False
d. False
False
False
True

11. Given:
1- public class JavaCertQType1 {
2- public static void main(String... cmd) {
3- int i = -3;
4- byte b = 5;
5- float f = 1e-10f;
6- double d = 3.14;
7- boolean b1 = i > i;
8- boolean b2 = i < b;
9- boolean b3 = b <= f;
10- boolean b4 = f >= d;
11- boolean b5 = d != 0;
12- boolean b6 = 1 == f;
13- System.out.println(b1);
14- System.out.println( b2);
15- System.out.println( b3);
16- System.out.println(b4);
17- System.out.println(b5);
18- System.out.println(b6);
19- }
20- }

Select correct options:


a. Code will compile successfully if code on line 6 is commented.
b. Code will output the same result if i=-3 is changed to i=-2.
c. Code will output the same result if i=-3 is changed to i=3.
d. Compilation error
e. Runtime exception

12. Given:

public class JavaCertQType1 {


public static void main(String... cmd) {
boolean b1 = true;
boolean b2 = false;
boolean b3 = b2 == b1 && b1 != b2 ;
System.out.println(b3);
}
}
What is the output?
a. Code output true.
b. Compilation error.
c. Code output False.

13. Given
public class JavaCertQType1 {

public static void main(String... cmd) {

boolean b1 = true;
int b=20;
//insert code

System.out.print(a==10 && b!=20);


System.out.print(b1!=true || b2!=false);

}
}

Which options, when inserted individually at //INSERT CODE HERE will


enable class JavaCertQType1 to output value falsetrue
a. boolean b2 = false; int a=10;
b. boolean b2 = true; int a=1;
c. boolean b2 = false; int a=1;
d. boolean b2 = true; int a=10;

14. What are the ways to create objects of all the wrapper classes?
a. Constructor, Setter, Getter.
b. Assignment, Static methods.
c. Assignment, Constructor, Static methods.
d. none of the above.

15. Given
public class JavaCertQType1 {

public static void main(String... cmd) {

Integer i1 = new Integer(42);


Integer i2 = new Integer ("42");
Float f1 = new Float(3.14f);
Float f2 = new Float ("3.14f");
System.out.println(i1==i2);
}
}
What is the output?

a. True
b. False

16. Given:
public class JavaCertQType1 {
public static void main(String... method) {
String result = "1";
double calificacion = 3;
if (calificacion < 3)
result = "Pierde materia";
else if (calificacion >= 3)
result = "Pasa la materia";
else if (calificacion > 5 )
result = "Error, la nota debe ser de 0.0 - 5.0";
System.out.println(result);
}
}

What is output:

a. Pierde la materia
b. Pasa la materia
c. Error, la nota debe ser de 0.0 - 5.0
d. Compilation error
e. Runtime exception
17. Given:

package ejemplo;

//insert code 1

public class JavaCertQType1 {

public static void main(String... method) {

Scanner scanner = new Scanner(System.in);


System.out.print("escanear edad:");

int edad = scanner.nextInt();

//insert code 2

else {
System.out.println("no puede beber");

System.out.print("mostrar la edad: ");

System.out.println(edad);

a.
Code 1: import java.util.Scanner;

Code 2:

if (edad >= 18) {

System.out.println("puede beber");
}

b.

Code 1: import java.util.Scanner.*;

Code 2:

if (edad >= 18) {

System.out.println("puede beber");

c.

Code 1: import java.util.Scanner;

Code 2:

if (edad >= 18) {

System.out.println("puede beber")

d.

Code 1: import java.util.lang;

Code 2:

if (edad >= 18) {

System.out.println("puede beber");

18. Given:

public class JavaCertQType1 {

public static void main(String... cmd) {


int n1 = 4000;
int n2 = 12;
int n3 = 8;
int n4 = 26;
int discount = {n1 > 3000}? {n2 > 11}? 10 : {n3 > 9}? {n4> 30}?
20 : 30 : 5 : 40;
int discount1 = {n1 > 5000}? {n2 > 9}? 10 : {n3 > 8}? {n4> 3}?
20 : 30 : 5 : 40;
System.out.println(discount);
System.out.println(discount1);
}
}

What is the output?


a. Code output
10
40
b. Code output
10
5
c. Compilation error.
d. Runtime exception
e. Code output
5
5

19. Given:

public class JavaCertQType1 {

public static void main(String... method) {

String usuario = "admin";

switch (usuario) {

case "admin":

System.out.println("welcome admin");
break;
case "usuario1":

System.out.println("welcome usuario1");
break;

case "usuario2":
System.out.println("welcome usuario2");

break;

case "invitado":

System.out.println("welcome usuario1");

break;

case "luis":

System.out.println("welcome luis");

break;

case "juan":

System.out.println("welcome juan");

break;

default:

System.out.println("Usuario Invalido?");

break;
}
}

Select correct options

a. Code will compile successfully if code on line 6 is commented.


b. Code will output the same result if code on line 7 is commented.
c. Code won't compile if code on line 7 delete.
d. The code compiles successfully.

20. Given:
package ejemplo;
import javax.swing.JOptionPane;
public class JavaCertQType1 {
public static void main(String... method) {
String numero = JOptionPane.showInputDialog("numero");
int n = Integer.parseInt(numero));
int resultado;
for(int i=1;i<=n;i++) {
for(int j=1;j<10;j++) {
resultado = i*j;
System.out.println(i + " x " + j + " = " + resultado);
}
}
}
}

Select incorrect options:

a. Code will compile successfully if code on line 2 is commented.

b. Code will output the same result if code on line 11 is commented.

c. Code won't compile if code on line 5 delete.

d. The code compiles successfully.


21. Given:
public class JavaCertQType1 {
public static void main(String... method) {
int n = 30;
int resultado;
for(int i=1;i<=n;i++) {
n--;
System.out.println(i);
}
}
}
what is the output?
a. 15
b. 30
c. 29
d. 14
e. Compile error
f. Runtime exception

Answer questions 22 and 23 in according with the following code:


1- public class JavaCertQType1 {
2- public static void main(String... method) {
3- String saludo="hola";
4- String[] n = new String[4];
5- for(int i=0;i<n.length;i++) {
6- n[i]=saludo + i;
7- }
8- for(String m:n) {
9- System.out.println(m);
10- }
11- }
12- }
22. what is the output?
a. hola0
hola1
hola2
hola3
b. hola3
hola2
hola1
hola0
c. hola1
hola2
hola3
hola4
d. Compile error

23. Select incorrect option:


a. Code will compile successfully if code on line 3 is commented.
b. Code will output the same result if code on line 6 is commented.
c. Code compile if code on line 3 is placed after code on line 4.
d. The code compiles successfully.

24. What's the difference between the While loop and the Do-While loop?
a. The two loops have no difference, the two work in the same way.
b. The while loops checks its condition before evaluating its loop body, and
the do-while loop checks its condition after executing the statements
defined in its loop body.
c. The while loops checks its condition before after executing the
statements defined in its loop body, and the do-while loop checks its
condition before evaluating its loop body.

25. Given:
public class JavaCertQType1 {
public static void main(String... method) {
int numero =1,i=1;
while(numero<30) {
numero=numero+1;
i++;
if(i>=12) {
break;
}
}
System.out.println(numero);
}
}
what is the output?
a. Code output 29.
b. Code output 30.
c. Code output 12.
d. Code won't compile.

26. Which loop to use when you don’t know the number of iterations
beforehand and when the number of iterations depends on a condition being
true?
a. for
b. enhanced for
c. while and do-while

Answer questions 27 and 28 in according with the following code:


1- public class JavaCertQType1 {
2- public static void main(String... method) {
3- String saludo="hola ";
4- String[] n = {"juan","luis","pedro","diego","ana"};
5- for(String m:n) {
6- if(m.equals("juan")) {
7- System.out.println(saludo + m);
8- continue;
9- } else {
10- System.out.println(saludo + m);
11- break;
12- }
13- }
14- }
15- }

27. what is the output?


a. Code output
hola juan
hola luis
b. Code output
luis
juan
c. Code output
hola
hola
d. Compile error

28. Select correct option:


a. Code will compile successfully if code on line 4 is commented.
b. Code will output the same result if code on line 11 is commented.
c. Code will output the same result if code on line 11 is placed after code
on line 8.
d. The code compiles successfully.

29. Given:
public class JavaCertQType1 {
public static void main(String... method) {
for (int i = 0; i < 3; i++) {
first:
for (int j = 0; j < 3; j++){
if(i == 1){
continue first;
}
System.out.print(" [i = " + i + ", j = " + j + "] ");
}
}
System.out.println();
}
}

what is the output?


a. [i = 0, j = 0] [i = 0, j = 1] [i = 0, j = 2] [i = 2, j = 0] [i = 2, j = 1] [i = 2, j = 2]
b. [i = 0, j = 0]
[i = 0, j = 1]
[i = 0, j = 2]
[i = 2, j = 0]
[i = 2, j = 1]
[i = 2, j = 2]
c. Compile error
d. Runtime exception

30. Given:
1- public class JavaCertQType1 {
2- public static void main(String... method) {
3- String[] programmers = {"luis", "fernando", "juan", "Pedro"};
4- String saludo ="hola ";
5- outer:
6- for (String name1 : programmers) {
7-
8- for (String name : programmers) {
9- if (name.equals("fernando"))
10- break outer;
11- System.out.println(saludo + name);
12- }
13- }
14- }
15- }
Select correct options:
a. Code will compile successfully if code on line 4 is commented.
b. Code will output the same result if code on line 9 is commented.
c. Code will output the same result if code on line 4 is placed after code on
line 5.
d. The code compiles successfully.

You might also like