Computer Science Java Quiz

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Salaheldein
S
Salaheldein
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,532
Questions: 23 | Attempts: 1,534

SettingsSettingsSettings
Computer Science Java Quiz - Quiz

.


Questions and Answers
  • 1. 

    Refer to the following variable declaration for this question: int[] numbers = {1,2,3,4,5,6,7,8,9,10}; Which of the following is a valid conclusion based on this variable declaration?

    • A.

      Int[8] numbers = 9;

    • B.

      Numbers[2] = 2;

    • C.

      Int[9] numbers = 10;

    • D.

      Numbers[5] = 6;

    • E.

      Both a and c

    Correct Answer
    D. Numbers[5] = 6;
    Explanation
    The valid conclusion based on the given variable declaration is that the value at index 5 of the array "numbers" is being changed to 6.

    Rate this question:

  • 2. 

    Refer to the following variable declaration for this question: double[] decimals = {0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0}; What does this variable declaration represent?

    • A.

      A package

    • B.

      A list

    • C.

      An iteration

    • D.

      An integer

    • E.

      None of the above

    Correct Answer
    B. A list
    Explanation
    The given variable declaration represents a list. It is an array of double values that stores decimal numbers ranging from 0.1 to 1.0. The use of square brackets and the presence of multiple values indicate that it is a list or an array.

    Rate this question:

  • 3. 

    When should one choose a for() loop instead of a while() loop?

    • A.

      When you have multiple conditions

    • B.

      When a condition will end the loop

    • C.

      When you know the number of times the loop should execute

    • D.

      When you are performing a specific java function

    • E.

      None of the above

    Correct Answer
    C. When you know the number of times the loop should execute
    Explanation
    A for() loop should be chosen instead of a while() loop when you know the number of times the loop should execute. The for() loop is designed to execute a specific number of times, which is determined by the initialization, condition, and increment/decrement statements within the loop structure. This makes it ideal for situations where the number of iterations is known in advance, such as iterating over arrays or performing a fixed number of calculations.

    Rate this question:

  • 4. 

    When running any java class, which of the following is the starting point?

    • A.

      The method header

    • B.

      The class header

    • C.

      The main() method

    • D.

      My_first_method()

    • E.

      None of the above - not enough information is given

    Correct Answer
    E. None of the above - not enough information is given
    Explanation
    The question asks about the starting point when running any Java class, but the given options do not provide enough information to determine the correct answer. The starting point for running a Java class is the main() method, which serves as the entry point for the program. However, since this option is not provided, none of the given options can be considered as the correct answer.

    Rate this question:

  • 5. 

    Each time that a loop "loops" is known as a(n):

    • A.

      Index

    • B.

      Count

    • C.

      Increment

    • D.

      Iteration

    • E.

      None of these choices

    Correct Answer
    D. Iteration
    Explanation
    In programming, a loop is a structure that repeats a certain block of code multiple times. Each repetition of the loop is known as an iteration. Therefore, the correct answer is "iteration".

    Rate this question:

  • 6. 

    Which of the following most specifically defines a package?

    • A.

      A group of java classes with a similar purpose

    • B.

      A group of java objects

    • C.

      A group of java methods with a similar purpose

    • D.

      A series of code blocks

    • E.

      A list of parameters

    Correct Answer
    A. A group of java classes with a similar purpose
    Explanation
    A package in Java is a way to organize and group related classes together. It allows for better organization and modularization of code. By grouping classes with a similar purpose, it becomes easier to manage and maintain the codebase. This also helps in avoiding naming conflicts and provides better code reusability. Therefore, the correct answer is "a group of java classes with a similar purpose".

    Rate this question:

  • 7. 

    Which of the following is an example of a method header?

    • A.

      Public void drawRectangle();

    • B.

      Public static int stringToInteger()

    • C.

      Public static calculateGCF()

    • D.

      DrawSquare(int scale);

    • E.

      All of the above

    Correct Answer
    B. Public static int stringToInteger()
    Explanation
    The given example "public static int stringToInteger()" is an example of a method header because it includes the access modifier "public", the keyword "static" indicating that the method belongs to the class rather than an instance of the class, the return type "int" indicating the type of value the method will return, and the method name "stringToInteger()".

    Rate this question:

  • 8. 

    An overloaded method is:

    • A.

      A method with an undefined type

    • B.

      A method with too many parameters

    • C.

      A method with the same name, but different parameters

    • D.

      A method called inside an infinite loop

    • E.

      A method with a syntax error

    Correct Answer
    C. A method with the same name, but different parameters
    Explanation
    An overloaded method is a method with the same name as another method, but with different parameters. This allows multiple methods with the same name to be defined in a class, each with a different set of parameters. The compiler distinguishes between these methods based on the number, type, and order of the parameters. This allows for more flexibility and versatility in method implementation, as different versions of the method can be called depending on the arguments provided.

    Rate this question:

  • 9. 

    Which of the following is an example of a call to a method to draw a rectangle that is 100 by 200 pixels?

    • A.

      Public void drawRectangle(100, 200);

    • B.

      DrawRectangle(100, 200);

    • C.

      Public void drawRectangle(int length, int width);

    • D.

      Public void drawRectangle(100, 200)

    • E.

      DrawRectangle(100, 200)

    Correct Answer
    B. DrawRectangle(100, 200);
    Explanation
    The correct answer is "drawRectangle(100, 200);" because it is the only option that correctly calls the method "drawRectangle" with the parameters "100" and "200", specifying the dimensions of the rectangle as 100 by 200 pixels.

    Rate this question:

  • 10. 

    Which of the following data types are ideal for numbers?

    • A.

      Float

    • B.

      Boolean

    • C.

      Char

    • D.

      Double

    • E.

      Both a and d

    Correct Answer
    E. Both a and d
    Explanation
    Both float and double are ideal data types for representing numbers with decimal points. Float is a 32-bit data type that can store decimal numbers with a precision of up to 7 digits, while double is a 64-bit data type that can store decimal numbers with a precision of up to 15 digits. Boolean is a data type used for representing true/false values, and char is used for representing single characters. Therefore, the correct answer is both a and d, as float and double are suitable for storing numbers.

    Rate this question:

  • 11. 

    Which of the following would be an example of only instantiating an object?

    • A.

      Turtle george = new Turtle();

    • B.

      George = new Turtle();

    • C.

      Turtle george;

    • D.

      Turtle = new Turtle(george);

    • E.

      None of the above

    Correct Answer
    B. George = new Turtle();
    Explanation
    The correct answer is "george = new Turtle();". This line of code is an example of only instantiating an object because it creates a new instance of the Turtle class and assigns it to the variable "george". The other options either declare a variable without instantiating an object or assign an existing object to a variable.

    Rate this question:

  • 12. 

    Which of the following would be an example of only declaring a number variable?

    • A.

      Double number;

    • B.

      Int number = 4;

    • C.

      Float number = new float;

    • D.

      None of the above

    Correct Answer
    A. Double number;
    Explanation
    The correct answer is "double number;" because it only declares a variable named "number" of type double without assigning any value to it. The other options either assign a value to the variable or create a new instance of a float variable.

    Rate this question:

  • 13. 

    Pseudocode is:

    • A.

      A formal programming language

    • B.

      Written after you have designed your program

    • C.

      Used by programmers to help map out what a program is supposed to do

    • D.

      Actual code which can be easily altered

    • E.

      None of the above

    Correct Answer
    C. Used by programmers to help map out what a program is supposed to do
    Explanation
    Pseudocode is a formal programming language that is written after the program design phase. It is used by programmers to help map out what a program is supposed to do. It is not actual code that can be executed, but rather a way to outline the logic and structure of a program before writing the actual code. Pseudocode allows programmers to plan and organize their code without worrying about the specific syntax of a programming language.

    Rate this question:

  • 14. 

    Which of the following is an example of a constructor?

    • A.

      Public class void SimpleTurtle

    • B.

      Public static int calculateGCF

    • C.

      Protected class Robot;

    • D.

      Private class Calculator

    • E.

      None of the above

    Correct Answer
    D. Private class Calculator
    Explanation
    A constructor is a special method in a class that is used to initialize objects of that class. It is typically declared with the same name as the class and has no return type. In this case, the answer "private class Calculator" is an example of a constructor because it is a class with the same name as the class it is defined in and it has the private access modifier, indicating that it is a constructor.

    Rate this question:

  • 15. 

    Refer to the following code for this question: public class HelloWorld {    public static void main (String [] args)    {       System.out.println("Hello World!!");    } } What would be the only appropriate file name for this program?

    • A.

      HelloWorld.java!

    • B.

      HelloWorld.java

    • C.

      Class HelloWorld.java

    • D.

      Public class HelloWorld.java

    • E.

      None of the above

    Correct Answer
    B. HelloWorld.java
    Explanation
    The only appropriate file name for this program would be "HelloWorld.java" because in Java, the file name must match the name of the public class defined in the file. In this case, the public class is named "HelloWorld", so the file name should be "HelloWorld.java".

    Rate this question:

  • 16. 

    Refer to the following code for this question: public class HelloWorld {    public static void main (String [] args)    {       System.out.println("Hello World!!");    } } What type of class is the class above?

    • A.

      A driver class

    • B.

      A driving class

    • C.

      An automatic class

    • D.

      A stand-alone class

    • E.

      None of the above

    Correct Answer
    A. A driver class
    Explanation
    The class above is a driver class because it contains the main() method, which serves as the entry point for the program. The main() method is responsible for starting the execution of the program and it is required in order to run the code. Therefore, this class can be considered as the driver or starting point of the program.

    Rate this question:

  • 17. 

    Following proper method naming conventions, which of the following would be a good method constructor?

    • A.

      Public static void drawSquare();

    • B.

      Private static int CalculateLCM()

    • C.

      Public void calculateNumberOfFactors()

    • D.

      Private void drawRectangle();

    • E.

      None of the above

    Correct Answer
    E. None of the above
    Explanation
    None of the above options are suitable for a method constructor. In Java, a method constructor should have the same name as the class, and it should not have a return type, not even void. 
    So, a proper method constructor would have a signature like public MyClass().

    Rate this question:

  • 18. 

    Which of the following would be used to import all the classes in the java.awt class?

    • A.

      Import java.util.*;

    • B.

      Import java.awt.%;

    • C.

      Import java.awt;

    • D.

      Import java.awt.*;

    • E.

      None of the above

    Correct Answer
    D. Import java.awt.*;
    Explanation
    The correct answer is "import java.awt.*;". This statement is used to import all the classes in the java.awt package. The asterisk (*) is a wildcard character that represents all classes in the package. By using this import statement, all the classes in the java.awt package can be accessed without specifying their individual names.

    Rate this question:

  • 19. 

    What is the use of the java.awt package?

    • A.

      For mainly allowing user-input to show up on the screen.

    • B.

      Creating user interfaces and for painting graphics and images.

    • C.

      To perform math functions such as multiplication or addition.

    • D.

      To determine the age of an individual based on the Gregorian calender

    • E.

      None of the above

    Correct Answer
    B. Creating user interfaces and for painting grapHics and images.
    Explanation
    The java.awt package is used for creating user interfaces and for painting graphics and images. This package provides classes and methods for creating windows, buttons, menus, and other graphical components. It also provides functionality for drawing shapes, images, and text on the screen. Therefore, the correct answer is creating user interfaces and for painting graphics and images.

    Rate this question:

  • 20. 

    Which of the following classes is included in the java.util package?

    • A.

      Color

    • B.

      Image

    • C.

      Scanner

    • D.

      Graphics2D

    • E.

      A,b and d

    Correct Answer
    C. Scanner
    Explanation
    The correct answer is Scanner. Scanner is a class included in the java.util package. It is used to read input from various sources like files, strings, and the console. It provides methods to parse primitive types and strings, making it useful for input validation and data extraction.

    Rate this question:

  • 21. 

    Which of the following best describes a contructor method?

    • A.

      A normal public method

    • B.

      A public method that shares the same name as the class, but cannot return a value

    • C.

      A private method which can only be performed if the user types in a password

    • D.

      A normal private method

    • E.

      None of the above

    Correct Answer
    B. A public method that shares the same name as the class, but cannot return a value
    Explanation
    A constructor method is a special type of method in object-oriented programming that is used to initialize objects of a class. It has the same name as the class and does not have a return type. It is typically declared as public and is used to set initial values for the instance variables of the class. Therefore, the answer "A public method that shares the same name as the class, but cannot return a value" accurately describes a constructor method.

    Rate this question:

  • 22. 

    Given the Turtle object t1, how can you call a method to make it move forward 100 pixels, using a distance parameter?

    • A.

      T1-forward();

    • B.

      T1.backward(-);

    • C.

      T1_forward(100);

    • D.

      T1.backward(-100);

    • E.

      None of the above

    Correct Answer
    D. T1.backward(-100);
    Explanation
    The correct answer is t1.backward(-100). This is because the backward() method is used to move the turtle in the opposite direction it is facing. In this case, the turtle is moved backwards by a distance of 100 pixels. The negative sign in the parameter is used to indicate the opposite direction.

    Rate this question:

  • 23. 

    Which of the following is not a primitive data type?

    • A.

      String

    • B.

      Int

    • C.

      Double

    • D.

      Char

    • E.

      None of the above

    Correct Answer
    A. String
    Explanation
    The data types int, double, and char are all primitive data types in programming. They are used to represent basic types of data such as whole numbers, decimal numbers, and individual characters, respectively. However, String is not a primitive data type. It is a class in Java and other programming languages that represents a sequence of characters. Unlike primitive data types, objects of the String class can have methods and properties associated with them.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jan 17, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Jan 24, 2012
    Quiz Created by
    Salaheldein
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.