Java CIS 109 Test 1

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 Xenonii
X
Xenonii
Community Contributor
Quizzes Created: 4 | Total Attempts: 973
Questions: 14 | Attempts: 304

SettingsSettingsSettings
Java Quizzes & Trivia

You will be tested on java constructs such as loops, if conditions, methods etc


Questions and Answers
  • 1. 

    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
    Explanation
    The correct boolean expression is "x > 1000 && x

    Rate this question:

  • 2. 

    Will this compile?class A{    System.out.println(20 * 10);}

    • A.

      Yes

    • B.

      No

    Correct Answer
    B. No
    Explanation
    No it won't because such a statement should be within some method, e.g. the main method.

    Rate this question:

  • 3. 

    Will this compile?class B{   public static void main(String[] args)   {      Scanner sc = new Scanner(System.in);   }}

    • A.

      Yes

    • B.

      No

    Correct Answer
    B. No
    Explanation
    No, because you need to import java.util.Scanner

    Rate this question:

  • 4. 

    What will this output?System.out.println(5/2);

    • A.

      2

    • B.

      2.5

    • C.

      Some kind of error

    • D.

      3

    Correct Answer
    A. 2
    Explanation
    Since you are dividing two integers, the result is also an integer. It simply doesn't output the decimal part of the result. If you wanted to show a decimal number, one of them needs to be a double. E.g. 3/2.0

    Rate this question:

  • 5. 

    What will this output?int i = 10;int j = 20;System.out.println("The result of "+j+" division by 2 is "+i+".");

    • A.

      The result of j division by 2 is i.

    • B.

      The result of 20 division by 2 is 10.

    • C.

      The result of10 division by 2 is 20.

    • D.

      None of the above

    Correct Answer
    B. The result of 20 division by 2 is 10.
    Explanation
    The given code will output "The result of 20 division by 2 is 10." This is because the code is using the variables `i` and `j` in the `System.out.println()` statement. The value of `j` is 20 and the value of `i` is 10. So when the code is executed, it will print the string "The result of 20 division by 2 is 10."

    Rate this question:

  • 6. 

    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:

  • 7. 

    Rewrite this as a for loopint i = 0;while (i < 10){   System.out.println(i);   i++;}

  • 8. 

    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:

  • 9. 

    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
    Explanation
    The return type of the given method body is "int" because the method is declared to return an integer value. The body of the method initializes an integer variable "i" with a value of 10 and then returns the result of multiplying 3 with "i", which is an integer value. Therefore, the correct answer is "int".

    Rate this question:

  • 10. 

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

    • A.

      Int

    • B.

      Long

    • C.

      Double

    • D.

      30.0

    • E.

      None of the above

    Correct Answer
    C. Double
    Explanation
    The return type of the given method is "double" because the method is declared to return a value of type "double" and the expression "3 * d" evaluates to a double value.

    Rate this question:

  • 11. 

    What is the return types of the following body of a method {   return "test "+123; }

    • A.

      Int

    • B.

      Char

    • C.

      String

    • D.

      Test123

    • E.

      Will not compile

    Correct Answer
    C. String
    Explanation
    The return type of the given method is String because the body of the method is returning a string value "test "+123. The return statement is used to return a value from a method, and in this case, the value being returned is a string.

    Rate this question:

  • 12. 

    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:

  • 13. 

    Write a program that will ask the user to start entering numbers. When he enters the number 0, you will show to him the sum of all the numbers he has entered.

  • 14. 

    What will be the output of the following programclass 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!!!
    Explanation
    The program will output "Welcome!!!" because the main method calls the getWelcomeMessage() method, which returns the string "Welcome!!!" and then prints it using the System.out.println() method.

    Rate this question:

Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.