0% found this document useful (0 votes)
10 views41 pages

Reviewer 1

The document is a reviewer for the MTA Prep Written Exam covering Java programming concepts. It includes multiple-choice questions on topics such as reading user input, variable scope, string manipulation, method creation, and basic arithmetic operations. Each question provides options for completing code snippets or determining outputs based on Java syntax and logic.
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)
10 views41 pages

Reviewer 1

The document is a reviewer for the MTA Prep Written Exam covering Java programming concepts. It includes multiple-choice questions on topics such as reading user input, variable scope, string manipulation, method creation, and basic arithmetic operations. Each question provides options for completing code snippets or determining outputs based on Java syntax and logic.
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/ 41

CCE102L/CCE109L Reviewer

MTA Prep Written Exam

Multiple Choice
Identify the choice that best completes the statement or answers the question

____ 1. You are creating a Java console application. You need to read a
birthdate entered by the user. How should you complete the code?

import (i)____________________;

public class DataReader


{
public static String getBirthdate()
{
System.out.println(“Enter your birthday in the format MMDDYYYY”);
(ii)_______________________________;

String birthdate = (iii)______________________;

(iv)_________________;

return birthdate;
}
}

a. (i) java.io.*;

(ii) InputStream stream = System.in;

(iii) stream.read();

(iv) stream.close();

b. (i) java.util.Scanner;

(ii) Scanner sc = new Scanner(System.in);

(iii) sc.next();

(iv) sc.close();
c. (i) java.io.*;

(ii) InputStream stream = System.in;

(iii) stream.mark(8);

(iv) stream.wait();

d. (i) java.util.Scanner;

(ii) Scanner sc = new Scanner(System.in);

(iii) sc.next();

(iv) sc.wait();

____ 2. public class JavaProgram1

int x = 25;

public static void main(String[] args)

JavaProgram1 app = new JavaProgram1();

int x=5;

int x=10;

int x = 100;

System.out.println(x);

System.out.println(app.x);
}

public JavaProgram1()

int x = 1;

System.out.println(x);

a. 1

100

25

b. 1

100

100

c. 1

10

100

d. 25

100

100
____ 3.
You are writing a Java program.
The program must meet the following requirements:
Truncate firstName to its first five characters
Set output to a string that contains the firstName and the number of characters
in firstName How should you complete the code? To answer, select the
appropriate code segments in the answer area.

public static void main (String[] args)

String firstName=”Christopher”;

FirstName=FirstName._____________; (i)

String output = String.format(“(ii) ____ is (iii) _______ characters long”,


firstName, firstName.___________(iv));

System.out.println(output);

a. (i) substring(0,5)

(ii) %d

(iii) %s

(iv) length()

b. (i) subSequence(5,0)

(ii) %s

(iii) %f

(iv) length()

c. (i) substring(5)

(ii) %s

(iii) %f

(iv) chars()
d. (i) substring(0,5)

(ii) %s

(iii) %d

(iv) length()

___ 4.
You are writing a Java method that evaluates an arithmetic formula.
The method accepts an int value named number, raises the value to the second
power, and returns the negative value of the result.
How should you complete the code? To answer, drag the appropriate code
segment to the correct position. Each code segment may be used once, more
than once, or not at all. You may need to drag the split bar between panes or
scroll to view content.

public static int negativeSquare(int number){


return (i) __________( (ii)________________ (iii) __________ (iv)
_____________);

a. (i) -

(ii) number

(iii) *

(iv) 2

b. (i) -

(ii) number

(iii) ^

(iv) 2
c. (i) -

(ii) number

(iii) *

(iv) number

d. (i) number

(ii) 2

(iii) *

(iv) number

____ 5. You are writing a Java method. The program must meet the following
requirements: Accept a String parameter firstName. Display a welcome
message that contains firstNam. Ensure that the first letter of the name
is capitalized, and the remaining letters are in lowercase How should
you complete the code? To answer, drag the appropriate code segment
to the correct position. Each code segment may be used once, more
than once, or not at all. You may need to drag the split bar between
panes or scroll to view content

public String showGreeting(String firstName)

String welcomeMsg = “Welcome, “;

(i) (ii)

welcomeMsg += firstName._______________(0, 1)._______________()+

(iii) (iv)

firstName.____________________(1).___________________();
a. (i) substring

(ii) toUpperCase

(iii) charAt

(iv) toLowerCase

b. (i) substring

(ii) toUpperCase

(iii) substring

(iv) toLowerCase

c. (i) charAt

(ii) toLowerCase

(iii) substring

(iv) toUpperCase

d. (i) charAt

(ii) toUpperCase

(iii) charAt

(iv) toLowerCase
____ 6. You are writing a Java console program. The program accepts
command line arguments. You need to ensure that the main method
parses and handles each command line argument. How should you
complete the code? To answer, select the appropriate code segments in
the answer area.

public static void main ( (i)_______________________ args)


{
for (int 1=0; i< (ii) _________________; i++)
{
handleArgument( (iii) ________________________);
}
}

a. (i) ArrayList

(ii) args.length - 1

(iii) args[i]

b. (i) String

(ii) args.length

(iii) args[i]

c. (i) String[]

(ii) args[i]

(iii) args.charAt(i)

d. (i) String[]

(ii) args.length

(iii) args[i]
____ 7. You are writing a Java method. The method must meet the following
requirements: Accept a String array named entries Iterate through
entries Stop the iteration and return false if any element has more than
10 characters Otherwise, return trueHow should you complete the
code? To answer, select the appropriate code segments in the answer
area.

public boolean validateEntries(String[] entries){

boolean allValidEntries = true;

_______________(i) (String entry ______________(ii) entries)

if (entry.length() > 10) {

allValidEntries = false;

_________________(iii)

a. (i) do

(ii) :

(iii) break;

b. (i) for

(ii) instanceof

(iii) continue;

c. (i) for

(ii) :

(iii) break;

d. (i) while

(ii) ++
(iii) break;

____ 8. You are creating a method that processes invoices. The invoices are
contained in an ArrayList instance. After each invoice is processed, the method
must remove the invoice from the ArrayList instance. How should you complete
the code? To answer, select the appropriate code segments in the answer area.

public static void Process(ArrayList<String> invoices)


{
for (int i= _________ (i); i< invoices.__________ (ii) ; ___________ (iii))
{
String invoice=invoices.get(i);
invoices.remove(i);
}
}

a. (i) 0

(ii) size()

(iii) i=i+1

b. (i) 0

(ii) size()

(iii) )

c. (i) 1

(ii) size()+1

(iii) i=i+1)

d. (i) 0

(ii) size()-1

(iii) i=i+1)
____ 9. What is the value of (2+3) * 4 - 1

a. 13

b. 19

c. 26

d. 2

____ 10. What is the value of 4 * 4 + 2 * 5

a. 90

b. 26

c. 15

d. 19

____ 11. What is the value of 8 * 2 % 3

a. 1

b. 0

c. 5

d. 26
____ 12. What is the value of 5 / 2 - 4 % 2

a. 2

b. 1

c. 2.5

d. 15

____ 13. You are writing a method that accepts an int parameter named start
and display all numbers from start to zero in decrements of one.

How should you complete the code?

public static void countdown(int start){

for (__________________; _____________; ___________)

System.out.println(i);

a. int i=start; i >=0; --i

b. int i==start; i >= 0; --i

c. int i = start; i > 0; ++i

d. int i = start; i < 0; ++i


____ 14. You should use an int data type to store the numeric value
3,000,000,000 (3 billion) so that the least amount of memory is used.

Review the underlined text.

a. no change is needed

b. should be changed to short data type

c. should be changed to byte data type

d. should be changed to long data type

____ 15. 1 public class Customer

2 {

3 private int id = 3;

4 public static void main (String[] args)

5 {

6 Customer customer = new Customer();

7 id=5;

8 showId();

9 }

10

11 protected void showId()


12 {

13 System.out.println(id);

14 }

15 }

The code does not compile. What should you do to correct the code?

a. change private int id =3 to public int id = 3 only

b. change protected void showId() to public void showId() only

c. change line 7 to customer.id=5 and line 8 to customer.showId()

d. change line 6 to customer Customer = new customer()

____ 16. class Pickle {

boolean isPreserved = false;

private boolean isCreated = false;

void preserve()

isPreserved = true;

public static void main (String[] args)

{
Pickle pickle = new pickle();

pickle.isCreated = true;

pickle.preserve();

The code cannot compile due to error. How can we modify it?

a. pickle Pickle = new pickle();

b. Pickle.isCreated = true;

c. pickle.preserve;

d. Pickle pickle = new Pickle();

____ 17. You work on a team that develops e-commerce applications. A


member of the team creates a class named db that connects to a
database. The class contains a method named query.

You need to instantiate the db class and invoke the query method. Which is
correct?

a. DB db = new DB; DB.query;

b. DB db = DB(); query();

c. DB db = new DB(); db.query();

d. DB db = new DB(); DB.query();


____ 18. You have the following initialization:

int num1 = 10;

int num2 = 20;

int num3 = 30;

You need to create an array named numbers initialized with num1, num2, num3.

Which is the right code?

a. int[] numbers = new int[] [num1, num2, num3];

b. int[] numbers = new int[] {num1, num2, num3};

c. new int[] numbers = int[] {num1, num2, num3};

d. new int numbers = int[] {num1, num2, num3};

____ 19. What is the output

double pi = Math.PI; // 3.141593

System.out.format(“PI is %.3f%n”, pi);

a. 3.142

b. 3.141
c. 3

d. 3.141593

____ 20. What is the output

double pi = Math.PI; // 3.141593

System.out.format(“PI is %.0f%n”, pi);

a. 3.

b. 3

c. 3.1

d. 0

____ 21. What is the output

double pi = Math.PI; // 3.141593

System.out.format(“PI is %09f%n”, pi);

a. 3.141593

b. 0000000003.141593
c. Error

d. 03.141593

____ 22. int a = 5;

int b = 10;

int c = ++a * b --;

System.out.println (c);

a. 15

b. 45

c. 60

d. 50

____ 23. int a = 5;

int b = 10;

int c = ++a * b --;

int d = a-- + ++b;

System.out.println(d);

a. 13
b. 14

c. 15

d. 16

____ 24. The following method must meet the requirements:

- accept an int array

- check for duplicate values in the array

- stop the outer loop as soon as duplicate value has been detected and return
true

- return false if all values in the array are unique

public static boolean duplicate(int[] array)

boolean isDup=false;

for (int x=0; x< array.length; x++){

for (int y=x+1; y< array.length; y++)

if (array [x] == array[y]

isDup= true;

if (isDup)

__________;

return isDup;

}
What should be the right code to complete this method?

a. false

b. true

c. break

d. continue

____ 25. Which of the following programs can be written with a switch
statement with up to three case statements?

Program 1

if (gpa == 4.0){

priority=1;

} else if (gpa >= 3.0){

priority=2;

} else if (gpa >=2.5){

priority=3;

Program 2

if (age >= 25) {


discount=0.50;}

else if (age >= 21) {

discount=0.25;

} else { discount=0.0;

Program 3

if (grade == “A”) {

message = “Exceeds Standards”;

} else if (grade == “B”) {

message = “Meets Standards”;

} else {

message = “Needs Improvement”;

a. Program 1 only

b. Program 2 only

c. Program 1 and 2 and 3

d. Program 2 and 3 only

____ 26.
String str = "Java";

StringBuffer buffer = new StringBuffer(str);

System.out.println(str+":"+buffer);

if (str.equals(buffer))

System.out.println("Both are equal");

else

System.out.println("Both are not equal");

What is the output?

a. Both are not equal

b. Both are equal

c. Compile time error as you cannot use equals for objects of different
classes

d. Runtime error as you cannot use equals for objects of different


classes

____ 27. public static int fee(char model) {

int price=0;

switch (model) {

case 'A': price=50;


break;

case 'T' : price = 20;

case 'C' : price=5;

break;

default : price=100;

break;

return price;

What is the value of price when the model is ‘A’?

a. 50

b. 20

c. 25

d. 100

____ 28. public static int fee(char model) {

int price=0;

switch (model) {

case 'A': price=50;


break;

case 'T' : price = 20;

case 'C' : price=5;

break;

default : price=100;

break;

return price;

What is the value of price when the model is ‘T’?

a. 50

b. 25

c. 20

d. 5

____ 29. public static int fee(char model) {

int price=0;

switch (model) {

case 'A': price=50;


break;

case 'T' : price = 20;

case 'C' : price=5;

break;

default : price=100;

break;

return price;

What is the value of price when the model is ‘S’?

a. 5

b. 100

c. 20

d. 50

____ 30. public static void main(String[] args) {

char digit = 'a';

for (int i=0; i<10; i++)

{
switch(digit)

case 'x': {
int j=0;
System.out.println(j);
}

default:
{
int j=100;
System.out.println(j);
}

int i=j;

System.out.println(i);

a. 100 will be printed 11 times

b. 100 will be printed 10 times and then there will be a runtime exception

c. The code will not compile because the variable i cannot be declared
twice within the main() method

d. None of these

____ 31. boolean a = true;

boolean b = false;

boolean c = true;
System.out.println(b=c);

if (a == true)

if (b==true)

if (c==true)

System.out.println("Some things are true");

else System.out.println("Nothing is true");

else if (a && (b=c))

System.out.println(("It is too confusing"));

else System.out.println("Hey this wont compile");

What is the output?

a. The code will not compile

b. “Some Things are true in this world” will be printed

c. “Hey this will not compile” will be printed

d. None of these

____ 32. String s1=new String("Test");

String s2=new String("Test");

if (s1==s2)
System.out.println("Same");

if (s1.equals(s2))

System.out.println("Equals");

What is the output?

a. Same

Equals

b. Equals

c. The code compiles, but nothing is displayed upon execution

d. The code fails to compilie

____ 33. public class Test

public void method()

for (int i=0; i < 3; i++)

System.out.print(i);

System.out.print(i);

}
a. 0122

b. 0123

c. Compilation error

d. None of these

____ 34. public class Ternary

public static void main(String[] args)

int a = 5;

System.out.println(“Value is - “ + ((a < 5)? 9.9: 9));

What is the output?

a. Value is - 7

b. Value is - 5

c. Compilation error

d. None of these
____ 35. int Output = 10;

boolean b1 = false;

if ((b1 == true) && ((Output += 10) == 20))

System.out.println("We are equal "+Output);

else

System.out.println("Not equal! "+Output);

What is the output?

a. Not equal! 10

b. Not equal! 20

c. We are equal 10

d. We are equal 20

____ 36. Which of the following code will print current time?

a. System.out.print(new LocalTime().now());
b. System.out.print(new LocalTime());

c. System.out.print(LocalTime.now());

d. System.out.print(LocalTime.today());

____ 37. Consider:

Integer number=Integer.valueOf(“808.1”);

Which of the following statement is true about the above code?

a. The value of the variable number will be 808.1

b. The value of the variable number will be 808

c. The value of the variable number will be 0.

d. A NumberFormatException will be thrown

____ 38. public class Whizlabs{

private String name;

private boolean pass;

public static void main (String[] args){

Whizlabs wb = new Whizlabs();

System.out.print(“name = “+wb.name);
System.out.print(“,pass=”+wb.pass);

a. name=,pass=

b. name=null,pass=false

c. name=null,pass=true

d. Compilation error

____ 39. public static void main(String[] args){

try

int whizData[] = new int[10];

System.out.println(“Accessing Element: “+whizData[11]);

catch(ArrayIndexOutOfBoundsException e){

System.out.println(“Oops.. Identified Exception:: “+e);

a. Compliation fails
b. Oops.. Identified Exception::
java.lang.ArrayIndexOutOfBoundsException: 11

c. Accessing Element : 0

d. Oops.. Identified Exception:: java.lang.NoDataFoundException

____ 40. You work as a Java Programmer. You need to convert a numeric
String to primitive double value.

What code segment should you use?

a. Double.valueOf(numberString);

b. double.parseDouble(numberString);

c. Double.parseDouble(numberString);

d. String.parseDouble(numberString);

____ 41. You have the following class definition:

class Logger

public void logError(String message)

{
}

The logError method can be invoked by code in all classes in the same package
as the Logger class

Review the underlined text. If it makes the statement correct, select “No change
is needed”. If the statement is incorrect, select the answer choice that makes the
statement correct.

No change is needed
a.

only by the Logger class


b.

only by the Logger class and classes in the same package that
c. inherit from it

by all classes in all packages


d.

____ 42. public static void main (String[] args)

double number = 27;

number %= -3d;

number += 10f;

number *= -4;

System.out.println(number);

}
a. -44 c. 40.0

b. -40.0 d. 44.0

____ 43. public void printInt()

if (true) {

int num=1;

if (num > 0) {

num++;

int num = 1;

addOne(num);

num = num -1;

System.out.println(num);

public void addOne(int num)

num = num +1;

What is the output when you run printInt()?


0
a.

1
b.

2
c.

3
d.

____ 44. You are interviewing for a job as a Java developer. You need to
evaluate the following code.

01 String s1=”Hello World”;

02 String s2=”Hello World”;

03 String s3=s2;

04

05

For each of the following statements, selects Yes if true.

(i) s1 and s2 refer to the same object in memory.

(ii) s2 and s3 refer to the same object in memory

(iii) A different string can be assigned to s1 on line 04.

(iv) A different string can be assigned to s2 on line 05.


(i) Yes
a. (ii) Yes
(iii) No
(iv) No

(i) No
b. (ii) No
(iii) Yes
(iv) Yes

(i) No
c. (ii) Yes
(iii) No
(iv) Yes

(i) Yes
d. (ii) No
(iii) Yes
(iv) No

____ 45. char data1=65;

System.out.println(data1);

A
a.

65
b.

a
c.

An exception is thrown
d.

____ 46. long data2=65;

System.out.println(data2);
65.0
a.

65
b.

a
c.

An exception is thrown
d.

____ 47. float data3 = new Float(“-65.0”);

System.out.println(data3);

65.0
a.

-65.0
b.

A
c.

An exception is thrown
d.

____ 48. short data4 = new Short(“-65.0”);

System.out.println(data4);

65.0
a.

-65.0
b.
A
c.

An exception is thrown
d.

____ 49.
Traders hires you to write a Java program to manage account openings.
To open a new account, a user must meet one the following requirements:
be over 65 years old and have a minimum annual income of 10,000
be at least 21 and have an annual income greater than 25,000
How should you complete the code?

(age > 65 && income > 10000 || age > 21 && income > 25000)
a.

(age > 65 && income < 10000 || age < 21 && income > 25000)
b.

(age > 65 && income < 10000 || age > 21 && income > 25000)
c.

(age > 65 && income >= 10000 || age >= 21 && income > 25000)
d.

____ 50. You need to evaluate the following code segment:

double dNum=2.667;

int iNum=0;

iNum=(int) dNum;

What happens when the code segment is run?

iNum has a value of 0


a.
An exception is thrown
b.

iNum has a value of 2


c.

iNum has a value of 3


d.

ANSWER:
1. B
2. A
3. D
4. C
5. B
6. D
7. C
8. A
9. B
10. B
11. A
12. A
13. A
14. D
15. C
16. D
17. C
18. B
19. A
20. B
21. D
22. C
23. D
24. C
25. C
26. A
27. A
28. D
29. B
30. D
31. D
32. B
33. C
34. D
35. A
36. C
37. D
38. B
39. B
40. C
41. C
42. B
43. A
44. A
45. A
46. B
47. B
48. D
49. D
50. C

You might also like