Java Foundations Certified Junior Associate 习 题, Exercises of Java Programming

(D) Underscores help the compiler interpret large numbers. Answer (B,C). The Java compiler automatically promotes byte, short, and chars data type values to ...

Typology: Exercises

2021/2022

Uploaded on 08/05/2022

dirk88
dirk88 🇧🇪

4.4

(222)

3.1K documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java Foundations Certified Junior Associate
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Java Foundations Certified Junior Associate 习 题 and more Exercises Java Programming in PDF only on Docsity!

Java Foundations Certified Junior Associate

  1. When the program runs normally (when not in debug mode), which statement is true about breakpoints? (A) Breakpoints will stop program execution at the last breakpoint. (B) Breakpoints will stop program execution at the first breakpoint. (C) Any Breakpoint will stop program execution. (D) Breakpoints will not have any effect on program execution. Answer (D)
  2. A breakpoint can be set by clicking a number in the left margin of the IDE. Clicking again removes the breakpoint. (A)True (B) False Answer (A)
  3. What is the purpose of adding comments in the code? (A) Provide good look and feel of the code. (B) To provide better security to the program. (C) It increases the execution time of the code. (D) Provide an explanation about the code to the programmer. Answer(D)
  4. System.in readies Scanner to collect input from the console. (A) True (B) False Answer (A) 5.Which two statements are true about the Scanner class? (Choose two correct answers) (A) A Scanner’s delimiter can be changed. (B) Scanners cannot read text files. (C) A Scanner object doesn’t have fields and methods. (D) A Scanner object opens a stream for collecting input. Answer( A,D)
  5. Import statements are placed above the class definition. (A) True (B) False Answer (A)
  6. Which is a risk of using fully qualified class names when importing? (A) Memory usage is increased. (B) The compiler runs longer. (C ) Performance of the code is reduced. (D) Code readability is reduced. Answer (D)
  7. Which two of the following statements are true? (Choose all correct answers) (A) Methods can be written with any number of parameters. (B) Methods can never be written with more than four parameters. (C) Methods cannot be written with parameters. (D) Parameter values can never be used within the method code block. (E) Parameter values can be used within the method code block. Answer (A,E)
  8. In Java, methods usually hold the properties of an object. (A) True (B) False Answer (B)
  9. Which two statements are true about the default statement? (Choose all correct answers)

(A) True (B) False Answer(A)

  1. A String can be created by combining multiple String Literals. (A) True (B) False Answer (A )
  2. Which is the correct declaration for a char data type? (A) char size = ’Medium’; (B) char size = “Medium”; (C) char size = “M”; (D) char size = ’M’; Answer (D)
  3. In Java, char is a primitive data type, while String is an object data type. (A) True (B) False Answer (A)
  4. Which two statements will not compile? (Choose all correct answers) (A) double salary = 20000.34; (B) int break=10; (C) double double=10; (D) int abc = 10; (E) int age=20; Answer (B,C)
  5. Which two are valid? (Choose all correct answers) (A) double doubleVar1, doubleVar2 = 3.1; (B) double doubleVar1 = 3.1; double doubleVar2 = 3.1; (C) double doubleVar1; doubleVar2 = 3.1. (D) double doubleVar1, double doubleVar2 = 3.1; Answer (A,B)
  6. Which of the following two statements are true about variables? (Choose all correct answers) (A) They make code becomes flexible. (B) The value assigned to a variable may never change. (C) Variables will be ignored by compiler. (D) They allow code to be edited more efficiently. Answer (A,D)
  7. How many bits are in a byte? (A) 2 (B) 4 (C) 6 (D) 7 (E) 8 Answer (E)
  8. Assuming x is an int, which of the following are ways to increment the value of x by 1? (Choose Three correct answers) (A) x = x +1; (B) x = +1; (C) x+; (D) x++; (E) x += 1;

Answer(A,D,E)

  1. What is the output? public class Person { public static void main(String args[]) { int age = 20; age = 5 + 3; age = age + 1; age++; System.out.println("Value of age: " +age); } } (A) Value of age: 20 (B) Value of age: 8 (C) Value of age: 10 (D) Value of age: 20 (E) Value of age: 28 (F) Value of age: 38 Answer (C)
  2. What is the package name which contains Math class? (A) java.net (B) java.lang (C) java.io (D) java.awt Answer (B)
  3. The Math class methods can be called without creating an instance of a Math object. (A) True (B) False Answer (A)
  4. You need to generate random integer values between 0 and 80 (inclusive). Which Random method should you use? (A) nextInt(); (B) nextInt(0-79); (C) nextInt(80); (D) nextInt(81); Answer (D)
  5. You need to generate random integer values in the range 2 through 10. This code fragment will produce the desired result. Random r = new Random(); r.nextInt(9) + 2; (A) True (B) False Answer (A)
  6. Which values are returned by the Random class method nextBoolean(); (A) An integer value. (B) Returns the next value. (C) Either a true or false. (D) Nothing is returned. Answer (C )
  7. Which statement is true about packages? (A) A package doesn’t contain a group of related classes. (B) Packages of the Java class library do not contain related classes.

Answer (B)

  1. What is the output? public static void main(String args[]) { String alphaNumeric = "Java World!" + 8; System.out.println(alphaNumeric); } (A) Java World! (B) Java World! + 8 (C) Java World! 8 (D) Compilation error. Answer (A)
  2. What is the output of the following code? public static void main(String args[]) { String firstString = "Java"; firstString = firstString.concat("World"); System.out.println(firstString); } (A) World (B) JavaWorld (C) Java World (D) Java Answer (B)
  3. Which method returns the length of a String? (A) compareTo() (B) length() (C) charAt() (D) findLength () Answer (B)
  4. The String concat() method concatenates only String data types. (A) True (B) False Answer (A)
  5. In a boolean expression which uses the && operator, what would make this expression evaluate to true? boolean x = (firstCondition && secondCondition); (A) If both the first condition and second condition are true (B) If the first condition is false, but the second condition is true (C) If both the first condition and second condition are false (D) If the first condition is true, but the second condition is false Answer (A)
  6. A customer is eligible for a discount based on certain criteria. Under what conditions does “You qualify for a discount” print? (Choose Two correct answers) int purchase; int rewardPoints; if (purchase >= 2000 || rewardPoints >= 4000) { System.out.println("You qualify for discount"); } (A) When rewardPoints is more than 2000 or purchase greater than 1000 (B) When purchase is 2000 regardless of the value of rewardPoints (C) When purchase is 4000 and rewardPoints is 2000

(D) When rewardPoints is more than 1000 and purchase is 1000 Answer (B,C)

  1. Which two are not logical operators? (Choose all correct answers) (A) || (B)! (C) % (D) + (E) && Answer (C,D)
  2. The switch statement is a more efficient way to write code when dealing with a large range of unknown values. (A) True (B) False Answer (B)
  3. What is the output? public static void main(String args[]) { char ch ='c'; switch(ch) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println("Vowels"); break; default: System.out.println("Consonants"); } } (A) Consonants (B) Vowels (C) Vowels (D) Compilation error Answer (A)
  4. A String comparison with == compares the Strings’ locations in memory and not the content of the String. (A) True (B) False Answer (A)
  5. Which three are conditional statements? (Choose all correct answers) (A) if statement (B) switch statement (C) if/else statement (D) for loop (E) do while loop Answer (A,B,C)
  6. Which operator is used to test if both sides of a boolean expression are equal? (A) == (B) = (C) >= (D) <=

(A) True (B) False Answer (A)

  1. When is an update expression in a for loop executed? (A) Before the first iteration through the loop. (B) Before each iteration through the loop. (C) After each iteration through the loop. (D) After two iterations through the loop. Answer (C)
  2. Which two statements are true about private access modifier? (Choose all correct answers) (A) Class fields marked private are most secure. (B) Class fields are typically marked public. (C) Class fields marked private are visible to any class. (D) Class fields are typically marked private. Answer (A,D)
  3. Which two statements are true? (Choose all correct answers) (A) An object can access another object’s main method. (B) An object can access another object’s public constructor. (C) An object can access another object’s public fields. (D) An object can access another object’s public methods. Answer (C,D)
  4. Which two are access modifiers? (Choose all correct answers) (A) final (B) private (C) public (D) static Answer (B,C)
  5. Given the following code, why does your IDE complain that “non-static variable name cannot be referenced from a static context”? public class Employee{ public static int employeeID; public String name; public static void display(){ System.out.println(employeeID); System.out.println(name); } } (A) Static variables cannot be referenced from methods. (B) The variable name has a null value. (C) It would be possible to call the display() method and attempt to reference an object’s name before any object exists. (D) Static variables are only accessible from instance methods. Answer (C )
  6. You never need to instantiate a Math object. (A) True (B) False Answer (A)
  7. A constructor is a special method which is commonly used to set the initial values of an object’s fields. (A) True (*) (B) False Answer (A)
  1. How could you write the Employee constructor so that its parameters are named the same as the fields they’re initializing? public class Employee{ private String name; private double salary; public Employee(String name, double salary){ //initialize name //initialize salary } } (A) public Employee(String name, double salary){ name = name; salary = salary; } (B) public Employee(String name, double salary){ name = this.name; salary = this.salary; } (C ) public Employee(String name, double salary){ this.name = name; this.salary = salary; } (D) public Employee(String name, double salary){ this.name = this.name; this.salary = this.salary; } Answer (C)
  2. If fields aren't initialized, they take on a default value. (A) True (B) False Answer (A)
  3. Which statement is true about the default constructor of a class? (A) Java automatically provides a constructor for every class. (B) You must write a default constructor. (C) The default constructor always returns void. (D) Default constructor should have at least one argument. Answer (A)
  4. Which has a default value of null? (A) boolean (B) int (C) String (D) double Answer (C)
  5. Class name should follow Camel casing rules. (A) True (B) False Answer (A)
  6. How can you retrieve a value from a method? (A) Pass a variable as an argument to the method. (B) Define the method return type as void
  1. Which of the following is not a wrapper class? (A) Integer (B) Boolean (C) String (D) Byte Answer (C )
  2. How could you declare an ArrayList so that it can store true or false values? (A) ArrayList<true, false> arrList = new ArrayList<>(); (B) ArrayList arrList = new ArrayList<>(); (C) ArrayList arrList = new ArrayList<>(); (D) ArrayList<True, False> arrList = new ArrayList<>(); Answer (C)
  3. Testing and debugging are important activities in software development. (A) True (B) False Answer (A)
  4. What are two disadvantages of adding print statements for debugging? (Choose all correct answers) (A) Print statements cannot print the values of variables. (B) Too many print statements lead to information overload. (C) It’s tedious to remove print statements. (D) Print statements cannot print the values of an object’s fields. Answer (B,C)
  5. Identify where there is a potential bug in this code: int radiusOfCircle = 10; int areaOfCircle = Math.PI * radiusOfCircle * radiusOfCircle; (A) A datatype is incorrect. (B) A variable hasn’t been assigned a value. (C) A variable name is misspelled. (D) A semi-colon is missing. Answer (A)
  6. Which is not a compilation error? (A) int y; y++; (B) x = ( 3 + 5; (C) y = 3 + * 5; (D) int x= Answer (A)
  7. What happens when you don’t handle an exception? (A) The execution of the program is terminated abruptly. (B) The program encounters error and simply ignores it. (C) A message is printed to the console to ask you how to handle the error. (D) All of the code after the error is skipped, but the program still runs. Answer (A)
  8. Each catch block is an exception handler that handles the type of exception indicated by its argument. (A) True (B) False Answer (A)
  9. An array allows you to create a single identifier that can be used to organize many items of the same data type. (A) True

(B) False Answer (A)

  1. What is the starting index of an array? (A) You can start with anything (B) It depends on the type of the array. (C) 0 (D) 1 Answer (C)
  2. What is an array? (A) An array is an indexed container that holds a set of values of a multiple types. (B) An array is a way to create multiple copies of a single value. (C) An array is a Java primitive type. (D) An array is an indexed container that holds a set of values of a single type. Answer (D)
  3. The Java compiler does not check for an ArrayIndexOutOfBoundsException during the compilation of a program containing arrays. (A) True (B) False Answer (A)
  4. In the OR (||) test, if the first expression on the left hand side is true then there is no need to evaluate the second statement. (A) True (B) False Answer (A)
  5. What is the result? public static void main(String[] args) { int point = 10; String s = (point == 1? "point" : "points"); System.out.println("I scored " +point +" " +s ); } (A) Compilation error (B) I scored 10 points (C) I scored 1 point (D) I scored 1 point 10 points Answer (B)
  6. What are the possible values of a boolean data type in Java? (A) yes/no (B) true/false (C) 0/ (D) good/bad Answer (B)
  7. If you need to make a particular variable belong to a class rather than any individual instance, what type of variable should you use?

(A) A private variable. (B) A public variable. (C) A local variable. (D) A static variable. Answer (D)

  1. An object must be instantiated before its non-static fields and methods can be accessed. (A) True (B) False