0% found this document useful (0 votes)
4 views32 pages

Java Quiz

The document consists of a series of quiz questions covering various topics in Java programming, including stakeholders, keywords, data types, object-oriented concepts, exception handling, collections, and more. Each question often includes multiple-choice answers or matching exercises to test understanding of Java principles. The content is structured to assess knowledge on Java syntax, programming concepts, and best practices.

Uploaded by

manvisingh2618
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
4 views32 pages

Java Quiz

The document consists of a series of quiz questions covering various topics in Java programming, including stakeholders, keywords, data types, object-oriented concepts, exception handling, collections, and more. Each question often includes multiple-choice answers or matching exercises to test understanding of Java principles. The content is structured to assess knowledge on Java syntax, programming concepts, and best practices.

Uploaded by

manvisingh2618
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Quiz Question Below are the main stakeholders we just discussed. Can you match each of them with the correct description? @ These are the correct matches. Supports testing of the software Oversees the product research from a UX perspective Oversees the product research and requirements Manages the development team Builds the software Description Stakeholder CTU Vea seu) Cree ECE cg 8 3 a Quiz Question Which of the following are true statements about keywords? (Select all that apply.) v| They are reserved words in the Java Language ‘They cannot be used as identifiers (e.g., when naming variables, classes, or methods) There are 70 keywords ‘There are 51 keywords in Java Quiz Question What would happen if we ran this code? int luckyNumber = 73 luckyNumber = "thre © _Itwould update the value of luckyNumber so that it contains the number 3 Itwould update the value of luckyNumber to the string of characters, oO “three” CO [tMould not work—because you must state the type of data used Inthe variable every time you update it (@ would not work—because the variables declared as an [ine] but then assigned a string value Quiz Question See if you can correctly indicate whether each of these keywords is used for a primitive or an object reference. @ These are the correct matches. Keyword Primitive or Reference int SY String yeas char SY double SN Integer eae Quiz Question Consider this code: double x = 35.75 int y = Gint)x3 If we print the value of y, what would we get? Question 1 of 2 When does truncation occur? EO Wher doing automatic type casting to convert larger type to a smaller type © when doing manual type casting to convert a larger type to a smaller type CE) When doing automatic type casting to convert a smaller type toa larger type © _ when doing manual type casting to convert a smaller type to a larger type Question 2 of 2 Truncation occurs because... O Java rounds larger values up © Smaller types allocate less memory than larger types O Java rounds smaller value down oO Java does not support type casting. Quiz Question Here's our example again: public double findTheArea(double length, double width) { double area = length * width; return area; Can you label each part? @ These are the correct matches. Code What is it? double area = length * width return area; Ltr double area = length * width return area; (double length, double width findTheArea return area; Body PETE ed OSM Rosella Quiz Question Each of the statements below either describes a stack or a heap. Can you identify which is which? @ These are the correct matches. Statement Stack or heap? Stores primitive types and object Se references. Stores Objects. Removes data in a LIFO manner when data is no longer needed. Global memory where data can be accessed from anyuhere in the ca program, Question 1 of 3 The protected access modifier can be accessed from the following: Berd (Select all that apply.) ¥j} From within the defining class ¥] From inside the same package as the defining class. From outside the package ¥]_ From outside the package by a subclass Question 2 of 3 ‘Suppose we have a class named Calculator and inside that class we define the following method: private void addNumber(int x, int y){ return x + y5 Now suppose that we try to call this method (e.g., addNunbers(2, 2) ) froma different class that is in a different package. Would this work? Oo No, the private access modifier would not be able to access this method unless both classes were in the same package. No, the private access modifier cannot be accessed from outside of the defining class. Yes, the private access modifier can be only accessed by other classes outside © Yes, the private access modifier can be accessed by any outside class. O tthe defining class's package. Question 3 of 3 Which of the following changes would allow the example above to work? © public void addnumber(int x, int y) © void addNumber(int x, int y) (default access) © Move the other class into the same package as Calculator. Question 1 of 3 The Service class has the public access modifier. This would mean the class can be accessed from? © onlya class in the same package. © Aciass that inherits from the Service class ©@ Aliciasses © None of the classes Question 2 of 3 In the Service class example, why is the method generateServiceld() private? © Toallow other classes to access it. ©@ To provide security so nobody outside of the class can access it. © Because the developer forgot to make it public. © Toallow only classes in the same packages to access it. Question 3 of 3 How should another class get the Service class ID? O By calling the variable directly. O By calling the method generateServiceld(). © By calling the method getServiceld(). O Other classes cannot get access to the Service ID. Quiz Question Given an array int[] numbers = {1, 2, 3, 4}3. Select the correct statement for assigning the value 3 in the array to a variable. © [int variable = numbers(o}; © [ine variabie = numbers [3]: © int variabie = numbers [2]; © [ine variatie = nunbers{e]; Question 1 of 2 What is the final value of i inside the following loop? for(int i = 0; i< 105 i++){ [Link](i); Question 2 of 2 Which loop is guaranteed to run at least once? Quiz Question Which of the following statements is correct? @ [heL2vade¢ tool reads through ave Hes and parses certain parts ofthe code to automatically generate useful documentation, © JavaDoc is a document format used to build in-memory apps. © The JavaDoc too! writes comments in java files. © The JavaDoc tool parses Java files and sends them to Java for v Quiz Question Which of the following statements about objects are true? (Select all that apply.) ‘We model software as objects to improve readability, maintainability and 4) reusability. ¥] Objects have two characteristics: state and behavior. ¥] Objects are an intuitive way to think about developing software. Quiz Question For each of the statements below, does it better describe a class or an object? @ These are the correct matches. Class or Object? Can essentially be used as a blueprint. Gee Isa specific instance. Object Can have a customized state. Colores Defines the behaviors that instances can have. Es Question 1 of 2 Below are the different parts of a class we just discussed. Can you match each part with the correct description? @ These are the correct matches. Description Part of the class ‘type of function used to model the behavior of the class. Guess Used to initialize objects during Classe instantiation. eee Used to provide state for the and bles Pe eee ee Seo Used to identify not only the class itself but also the associated ava file. Question 2 of 2 What does the Final keyword mean when it is used for a class? ‘The class can be overridden The class can only be inherited from another class in the same package. The class cannot be changed, overridden, or inherited from. O@®@OoO0O The class can be changed by any other class. Question 1 of 3 Making a Class, a Singleton will ensure that there is? O Multiple instances of the same class © There are exactly two instances ©@ There is only one instance of the class in the JVM © Does not change how the is stored in the JVM. Question 2 of 3 To make a class a Singleton, we do the following? © Make the constructor public so anyone can create an instance. © Make the constructor private and create a static reference. © Use the final keyword with the class. © Create a default constructor. Question 3 of 3 We create a Singleton class to ensure? O Objects using the class will get different state data. © Objects using the class will get the same state data. O There is no reason to create a singleton class. O Singleton classes are better for the VM. Quiz Question Please select all of the correct statements about Garbage Collection (GC). (Select all that apply.) v Garbage collections runs as a background task. Developers do not need to take any action to make sure Garbage Collection removes unused Objects from Heap memory. Garbage Collection may remove objects that stil have references. Garbage Collection only removes objects that no longer have references to them. Quiz Question In Java, packages are... (Select all that apply.) ¥]_ Used to organize and manage Java files. ¥| Used to distinguish Java files with the same name. Used to store metadata about Java files. Contain names for locations where objects are referenced in code. Quiz Question Why do we use inheritance? (Select all that apply.) To share state only and not behavior. ¥] To promote code reusability and reduce duplicate code. ¥] To create relationships between classes, going from general to specific. To promote code duplication. ¥| To share state and/or behavior with other related classes. Quiz Question Select the statement that is true about polymorphism. © Polymorphism creates an environment where software can be coupled by duplicating code. © Polymorphism is the ability for an object to take on many forms. © Polymorphism is when objects are forced to be a specific data type. © Polymorphism creates code and coupling by duplicating code. Quiz Question For each of the descriptions below, does it better describe an interface or an abstract class? @ These are the correct matches. Description Interface or Abstract class We expect unrelated classes will be implementing our super class. Se We want to support multiple Inter inheritance. inhi We want subclasses to have rR instance variables. CEP ERE ES We want to provide concrete rR methods for subclasses CEP ERE ES We want to make use of both concrete methods and abstract methods for subclasses. Question 1 of 3 All frontend application code requests should pass through which object? © SchedulingService © Room © Database © Menuart Question 2 of 3 The Room Model object should be accessed by which object? © SchedulingService O Room O Database © Menuet Question 3 of 3 Which of the following objects should be allowed to access the database? © Schedulingservice Room © © Database O MenuéP! Quiz Question Which of the following are true statements about exceptions and errors? (Select all that apply.) v| ‘The Exception and Error classes inherit from the Throwable class. Error are thrown for catastrophic events where the application is not expected to recover. Exceptions are thrown for problems where the application is expected to recover. ‘Throwing an Exception is when an Exception object is created and handed off to the Java Runtime. Throwing an Exception is when an Exception is created and handed off to the Garbage Collector to handle. Quiz Question Which of the following are true statements about handling exceptions? (Select all that apply.) v Java uses three different blocks for Exception handlers: try, catch and finally. Exception handlers typically handle a specific type of Exception class. Exception handlers should be used to handle Error class types. Exceptions are used to catch misformatted code. Exceptions are handled by creating an Exception Handler. Quiz Question Select the correct statement about enumerations (enums). © Enum are used to provide unit testing © Tums are a spedal datatype of constants that allow a variable tobe st from an enumerated list. © Enum provide a method structure for getting variables. © Enumscan only be embedded in a class and not created as a standalone class, Question 1 of 2 Which of the following statements are true about the Scanner class? (Select all that apply.) ‘The Scanner class reads data from methods to determine is valid. We use the Scanner class to create documents that can scanned and parsed, ¥] The Scanner class can be used to read input from the console. Q@ [(¥] The Scanner class can also use RegEx to parse console input. @ Match the code with the correct output for the String "1 number"; @ These are the correct matches. Code Output [Link]() Sg scanner. next() while([Link]()) { system. out. print1n(scanner } Dug [Link]() Quiz Question For each of the descriptions below, does it better describe the Date class or the Calendar class? @ These are the correct matches. Description Class Represents only a specific day and dia Date Represents a specific day and time and provides transformation fear methods. Has methods to set a specific Day, Month and Year. re TT Is an abstract class eure Isa concrete class Date Quiz Question Select the correct regular expression string to enforce a valid email address, such as: jeff@[Link] If you like, you can try out the patterns on [Link]. O *%#).coms" © Hos" © "123@(+).coms" O "HecHaz!" O “[1-91@(.+)[1-915" Question 1 of 4 Select the correct statements related to String management in Java. (Select all that apply.) Only one variable can point to a single String object. ¥] Multiple variables can point to the same String object. Changing a String variable’s value changes the String object's value. ¥] String are immutable. Question 2 of 4 ‘Suppose we declare a new String variable: String message = "foo"; And then later, we change the value of the string in this variable, like so: message = "bar"; When we assign this new string, “bar”, to the message variable, what happens in the computer's memory? The original String object is simply updated, so that it now contains the String oO “bar” in place of “foo” The original String object is left unchanged (it still contains the string| "foo" ), © but a new String object is created that contains the new String, "bar" Question 3 of 4 Suppose we have the following String variable: String text = "I think this is correct"; Which of the following pieces of code would tell us whether the string contains the substring "this"? © text. charat("this"); © text-equalsignorecase("this”); [Link]("this"); text. substring("this"); text. split(" OO © Question 4 of 4 Suppose we have this String variable: String text = "Happy Days are here again"; How could we create the substring “Happy”? oO [Link](@, 5); © [Link](e, 3); ©@ [Link](e, 5); © [Link](e, 4); Quiz Question Select the correct option for creating a List of Integers using generics. © List number = new ArrayList()5; © List number = new ArrayList(); @ Listcinteger> number = new arrayList(); C© |Listcint> number = new arrayList(); Quiz Question Which of the statements below are true about collections? (Select all that apply.) v v Collection inherits from the Iterable interface. Map, List, Set and Queue all implement the Collection interface. Collection is a concrete implementation of the Iterable interface. The array data structure implements the Collection interface. Collection is an Interface. Quiz Question Select the correct syntax for sorting an ArrayList of Strings called names . O oO O00 © names. sort() 5 sort .names(); [Link](names); Collector (names); Collector. sort(names); Quiz Question Which of the following are true statements about maps? (Select all that apply.) ¥]} Maps store data in a Key-value pair. | Maps can retrieve values in constant time. Maps do not implement the Collection interface v¥| Maps provide a list of all keys. “| Maps provide a list of all values. Quiz Question Which of the following statements are true about Sets? (Select all that apply.) ¥] Sets implement the Collection interface. ¥] Sets enforce no duplicate values. Sets store a key-value pair. Sets can be populated from another data list that implements the Collection interface, Sets allow duplicate values. Quiz Question Which of these statements are true about Queues? Select all that apply.) ~} Queues insert to the end of the Queue and pull from the top of the queue. ¥] Queues support all of the methods of the Collection interface. Queues enforce no duplicates. Queues store data in a Key-Value pair. ¥] Queues follow a First in and First out (FIFO) concept. Question 1 of 2 In the example code above, we have the People object. Select the correct equals method to sort objects by their address. @Override public boolean equals(Object 0) { aif (this 0) return true; © if (0 == null || getClass() People people = (People) o; return [Link]([Link]); [Link]()) return false; @Override public boolean equals(Object 0) { aif (this 0) return true; Oo if (0 == null || getClass() != [Link]()) return false; People people = (People) 0; return address. equals([Link]); oO @override public boolean equals(object 0) { if (this == 0) return true; if (0 == null [| getClass() ! People people = (People) 0; return [Link]([Link]); [Link]()) return false; @override public boolean equals(Object 0) { if (this == 0) return true; if (0 == null || getClass() People people = (People) 0; return [Link](people. name) ; [Link]()) return false; Goverride public boolean equals(Object 0) { if (this == 0) return true; if (0 == null || getClass() != [Link]()) return false; Question 2 of 2 In the People class example above. Select the correct example for overriding the hashcode. @override O public int hashCode() { return [Link](name); @Override © public int hashCode() { return [Link](address) ; @0verride O public int hashCode() { return address

You might also like