Quiz In Java

38 Questions | Attempts: 118
Share

SettingsSettingsSettings
Java Quizzes & Trivia

Test your Knowledge in Java by answering the questions. Once your done you will find out your own strengths and weekness.


Questions and Answers
  • 1. 

    8.2 Q1: The _________ of a class are also called the public services or the public interface that the class provides to its clients.

    • A.

      Public constructors.

    • B.

      public instance variables.

    • C.

      public methods

    • D.

      All of the above.

    Correct Answer
    C. public methods
  • 2. 

    Which of the following should usually be private?

    • A.

      Methods.

    • B.

      Constructors

    • C.

      Variables (or fields).

    • D.

      All of the above.

    Correct Answer
    C. Variables (or fields).
  • 3. 

    Which of the following statements is true?

    • A.

      Methods and instance variables can both be either public or private.

    • B.

      Information hiding is achieved by restricting access to class members via keyword public.

    • C.

      The private members of a class are directly accessible to the client of a class.

    • D.

      None of the above is true.

    Correct Answer
    A. Methods and instance variables can both be either public or private.
  • 4. 

    Having a this reference allows:

    • A.

      A method to refer explicitly to the instance variables and other methods of the object on which the method was called.

    • B.

      A method to refer implicitly to the instance variables and other methods of the object on which the method was called.

    • C.

      An object to reference itself.

    • D.

      All of the above.

    Correct Answer
    D. All of the above.
  • 5. 

     A constructor cannot:

    • A.

      Be overloaded.

    • B.

      Initialize variables to their defaults.

    • C.

      Specify return types or return values.

    • D.

      Have the same name as the class.

    Correct Answer
    C. Specify return types or return values.
  • 6. 

    8.5 Q2: Constructors:

    • A.

      Initialize instance variables.

    • B.

      When overloaded, can have identical argument lists.

    • C.

      When overloaded, are selected by number, types and order of types of parameters.

    • D.

      A and c.

    Correct Answer
    D. A and c.
  • 7. 

     A programmer-defined constructor that has no arguments is called a ________.

    • A.

      Zero-argument constructor.

    • B.

      No-argument constructor.

    • C.

      Default constructor

    • D.

      Main constructor.

    Correct Answer
    B. No-argument constructor.
  • 8. 

     Composition is sometimes referred to as a(n) ________.

    • A.

      Is-a relationship.

    • B.

      Has-a relationship.

    • C.

      Many-in-one relationship.

    • D.

      One-to-many relationship

    Correct Answer
    B. Has-a relationship.
  • 9. 

    Static class variables:

    • A.

      Are final.

    • B.

      Are public

    • C.

      Are private

    • D.

      Are shared by all objects of a class

    Correct Answer
    D. Are shared by all objects of a class
  • 10. 

     Which syntax imports all static members of class Math?

    • A.

      Static import java.lang.Math.*.

    • B.

      Import static java.lang.Math.*.

    • C.

      static import java.lang.Math

    • D.

      Import static java.lang.Math

    Correct Answer
    B. Import static java.lang.Math.*.
  • 11. 

    A package is:

    • A.

      A directory structure used to organize classes and interfaces.

    • B.

      A mechanism for software reuse

    • C.

      A group of related classes and interfaces

    • D.

      All

    Correct Answer
    D. All
  • 12. 

     A class within a package must be declared public if

    • A.

      It will be used only by other classes in the same package

    • B.

      It will be used by classes that are not in the same package

    • C.

      It is in the same directory as the other classes in the package.

    • D.

      It has a unique name

    Correct Answer
    B. It will be used by classes that are not in the same package
  • 13. 

     Consider the statement     package com.deitel.jhtp6.ch08;     Which of the following is true?

    • A.

      The statement declares a package that exists at deitel.com

    • B.

      The statement uses the Sun Microsystems convention of package naming

    • C.

      The statement should be placed inside the class declaration

    • D.

      The statement will generate a compile time error

    Correct Answer
    B. The statement uses the Sun Microsystems convention of package naming
  • 14. 

    The import declaration import java.util.*; is known as a ________.

    • A.

      Single-type-import declaration.

    • B.

      All-type-import declaration

    • C.

      Multiple-import declaration

    • D.

      Type-import-on-demand declaration

    Correct Answer
    D. Type-import-on-demand declaration
  • 15. 

     The import declaration import *; ________.

    • A.

      Causes a compilation error

    • B.

      Imports all classes in the library.

    • C.

      Imports the default classes in the library

    • D.

      Imports the classes in package java.lang

    Correct Answer
    A. Causes a compilation error
  • 16. 

    Which Man class properly represents the relationship "Man has a best friend who is a Dog"? A. class Man extends Dog { } B. class Man implements Dog { } C. class Man { private BestFriend dog; } D. class Man { private Dog bestFriend; } E. class Man { private Dog; } F. class Man { private BestFriend; }

    • A.

      A

    • B.

      B

    • C.

      C

    • D.

      D

    • E.

      E

    • F.

      F

    Correct Answer
    D. D
  • 17. 

    What will be the output of the following program class A {    public static void main(String[] args)    {       System.out.println(getWelcomeMessage());    }    static String getWelcomeMessage()    {       return "Welcome!!!";    } }

    • A.

      GetWelcomeMessage()

    • B.

      GetWelcomeMessage

    • C.

      Welcome!!!

    • D.

      It would give a compilation error

    Correct Answer
    C. Welcome!!!
  • 18. 

    What will the following output on the screen? for (int i = 0; i < 5; i++) {    System.out.print("*"); }

    • A.

      * * * * *

    • B.

      *****

    • C.

      ******

    • D.

      * * * * * *

    • E.

      None of the above

    Correct Answer
    B. *****
    Explanation
    It will only output 5 stars one after the other on the same line, because print is being used and not println. The counter i starts from 0, then it will execute the body of the for loop while i is less than 5. It would have output 6 stars if it was i

    Rate this question:

  • 19. 

    What is the return types of the following body of a method {    int i = 10;    return 3 * i; }

    • A.

      Int

    • B.

      Long

    • C.

      Double

    • D.

      30

    • E.

      None of the above

    Correct Answer
    A. Int
  • 20. 

    Will the following compile? int i = 0; for (    ; i < 5 ;   ) {    System.out.println(i);    i++; }

    • A.

      Yes

    • B.

      No

    Correct Answer
    A. Yes
    Explanation
    The for loops consists of
    for (; ; )
    All of these are optional and you can leave any of them out. Try experimenting it on BlueJ.

    Rate this question:

  • 21. 

    What will this output? System.out.println(4 + 5 * 2);

    • A.

      14

    • B.

      18

    • C.

      None of the above

    Correct Answer
    A. 14
    Explanation
    The * operator has a higher precedence than the plus operator

    Rate this question:

  • 22. 

    Which is the correct boolean expression which gives the following meaning: The value of x is greater than 1000 and x is smaller or equal to 2000

    • A.

      X >= 1000 && x

    • B.

      X > 1000 && x < 2000

    • C.

      X < 1000 && x >= 2000

    • D.

      X > 1000 && x

    • E.

      None of the above

    Correct Answer
    D. X > 1000 && x
  • 23. 

    Which of the following is NOT a strength of Java language?

    • A.

      Portable

    • B.

      Cheap

    • C.

      Relatively Simple

    • D.

      High Speed Execution

    Correct Answer
    D. High Speed Execution
  • 24. 

    Which of the following is NOT a Java program?

    • A.

      Applets

    • B.

      Servlets

    • C.

      Javascript

    • D.

      JavaBeans

    Correct Answer
    C. Javascript
  • 25. 

    What is the file extension of your Java source file?

    • A.

      .class

    • B.

      .java

    • C.

      .jar

    • D.

      .out

    Correct Answer
    B. .java
  • 26. 

    The Java compiler translate the Java code into ______

    • A.

      Bitcode

    • B.

      Bitecode

    • C.

      Bytecode

    • D.

      Bycode

    Correct Answer
    C. Bytecode
  • 27. 

    In order to execute a Java application, you need to have ______ on your computer

    • A.

      Java Virtual Machine

    • B.

      Java Real Machine

    • C.

      Java Running Machine

    • D.

      Java Verbal Machine

    Correct Answer
    A. Java Virtual Machine
  • 28. 

    Which of following is NOT one of the steps of program development life cycle discussed in the class?

    • A.

      Define the problem

    • B.

      Code the program

    • C.

      Install the program

    • D.

      Test and debug the program

    Correct Answer
    C. Install the program
  • 29. 

    Assembly language has one-to-one mapping to the machine language code

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
  • 30. 

    Which of the following Java programs can only be run using Web browser?

    • A.

      Servlet

    • B.

      Applet

    • C.

      JavaBeans

    • D.

      EJB

    Correct Answer
    B. Applet
  • 31. 

    The Java language was developed by __________

    • A.

      Java Microsystem

    • B.

      Sun Macrosystem

    • C.

      Java Macrosystem

    • D.

      Sun Microsystem

    Correct Answer
    D. Sun Microsystem
  • 32. 

    Each Java program will have at least ___ class(es).

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      4

    Correct Answer
    A. 1
  • 33. 

    How do you define a comment in Java code?

    • A.

      //

    • B.

      *()

    • C.

      /* and *//

    • D.

      Both a and c

    Correct Answer
    D. Both a and c
  • 34. 

    What will be the output of the program? public class X {     public static void main(String [] args)     {         try         {             badMethod();              System.out.print("A");         }          catch (Exception ex)         {             System.out.print("B");         }          finally         {             System.out.print("C");         }          System.out.print("D");     }      public static void badMethod() {} }

    • A.

      AC

    • B.

      BC

    • C.

      ACD

    • D.

      ABCD

    Correct Answer
    C. ACD
  • 35. 

    Class X implements Runnable {     public static void main(String args[])     {         /* Missing code? */     }     public void run() {} } Which of the following line of code is suitable to start a thread ?

    • A.

      Thread t = new Thread(X);

    • B.

      Thread t = new Thread(X); t.start();

    • C.

      X run = new X(); Thread t = new Thread(run); t.start();

    • D.

      Thread t = new Thread(); x.run();

    Correct Answer
    C. X run = new X(); Thread t = new Thread(run); t.start();
  • 36. 

    public class Test { } What is the prototype of the default constructor?

    • A.

      Test( )

    • B.

      Public Test( )

    • C.

      Test(void)

    • D.

      Public Test(void)

    Correct Answer
    B. Public Test( )
  • 37. 

    Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

    • A.

      Final

    • B.

      Private

    • C.

      Protected

    • D.

      Default

    Correct Answer
    B. Private
  • 38. 

    Which of the following group consists of only Object-Oriented languages?

    • A.

      C,C++,JAVA

    • B.

      C,JAVA,RUBY

    • C.

      C++,JAVA,BASIC

    • D.

      BASIC,FORTRAN,JAVA

    • E.

      C++,JAVA,C#

    Correct Answer
    E. C++,JAVA,C#

Quiz Review Timeline +

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

  • Current Version
  • Feb 19, 2012
    Quiz Edited by
    ProProfs Editorial Team
  • Feb 19, 2012
    Quiz Created by
    Mooly.89
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.