Category: Last published

Java puzzle 12: Passing a parameter to a constructor

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

In this Java puzzle we pass a parameter to a constructor. I congratulate you if you can solve this puzzle!

Java puzzle 12: Passing a parameter to a constructor

Level: Hard

What is the output of this program and why?

public class MyClass
{
  int x = 7;
  
  public MyClass()
  {
    x++;
    System.out.print(" n" + x);
  }
  public MyClass(int i)
  {
    x += i;
    System.out.print(" p" + x);
  }
  public int method(int i)
  {
    x += i;
    System.out.print(" s" + x);
    return x;
  }
  public static void main(String[] args)
  {
    new MyClass(new MyClass().method(3));
  }
}
Author: Sar Maroof

Answer explanation

  1. The statement new MyClass().method(3); instantiates an object from the class MyClass using the no-argument constructor.
    The statement x++; increments the value of x by 1, x = 7+ 1 = 8. The statement System.out.print(” n” + x); writes n8 to the standard output.
  2. The statement new MyClass().method(3) invokes the method and passes the value 3 to it. By passing the value 3, the statement x += i; increments the value of x by 3. The last value of x was 8. So, x = 8 + 3 = 11.
    The statement System.out.print(” s” + x); writes s11 to the standard output.
  3. The method method(3) returns 11 and we pass that value to a new object by using the statement new MyClass(new MyClass().method(3));
    new MyClass(11) creates an object using the one-argument constructor.
    The statement x += i; increments the value of x by i.
    Don’t forget that this is a new object that is why the value of x is 7 + 11 = 18.
    The statement System.out.print(” p” + x); writes p18 to the standard output.

The correct answer is: This program writes n8 s11 p18 to the standard output.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 14: Comparing strings

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

As a Java programmer you need very often to work with strings and you need to know how Java handles them. Here is a simple quiz that shows the way Java handles strings.

Java quiz 14: Comparing strings

Level: Normal

What happens when the following program is compiled and run?

public class MyString
{
  public static void main(String[] args)
  {
    String str1 = new String("Java");
    String str2 = new String("Java");
    String str3 = new String("java");
    if(str1 == str2)
    {
      System.out.print("w");
    }
    if(str1.equals(str2))
    {
      System.out.print("x");
    }
    if(str2.equals(str3))
    {
      System.out.print("y");
    }
    else
    {
      System.out.print("z");
    }
  }
}
  Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. The first conditional statement if (str1 == str2) checks whether str1 and str2 are references of the same object, but they are not.
    Therefore the body is not executed and w is not written to the standard output.
  2. The statement if (str1.equals(str2)) compares str1 and str2 whether their contents are equal. That returns true, because the content
    of both statements are Java. So, the letter x is written to the standard output.
  3. The statement if(str2.equals(str3)) compares str2 and str3 whether their contents are equal.That returns false,
    because the content of str2 is Java while the content of str3 is java starts with small letter.
  4. Since the last if statement returns false the else statement is executed and z is written to the standard output.
  5. The program writes xz to the standard output.

The correct answer is: c.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 11: Operators and conditional statements

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

The combination of operators and conditional statements appear in almost every program. Every Java programmer should master all types
of conditional statements. Therefore, I focus to offer that in different ways in my puzzles and quizzes. Please, always check out the explanation of the answer if you have doubts.

Java puzzle 11: Operators and conditional statements

Level: Easy

What is the output of this program and why?

public class Operator
{
  public static void main(String[] args)
  {
    int i = 2;
    int i2 = 4;
    int i3 = 7;
    if(i + i2 > i3 || i2 > i3 || i + i3 >= 9)
    {
      if(i2 == i + 2 && i2 < i)
      {
        System.out.print("x");
      }
      System.out.print("z");
    }
    else
    {
      System.out.print("y");
    }
  }
}
Author: Sar Maroof

Answer explanation

  1. The first if statement if(i + i2 > i3 || i2 > i3 || i + i3 >= 9) uses the conditional operator || (OR). By using || operator, the condition is true if one of the operands returns true.
    i + i2 > i3 returns false. i2 > i3 returns false. i + i3 >= 9 returns true. Therefore the if block is executed and the else block is ignored.
  2. By using the conditional operator && (AND) see the second statement if(i2 == i + 2 && i2 < i)  both operands must return true otherwise the block wouldn’t be executed.          i2 == i + 2 returns true.
    i2 < i returns false. Therefore the block is not executed and the letter x is not written to the standard output.
  3. The statement System.out.print(“z”); is inside the first conditional statement, which returns true. Therefore, the letter z is written to the standard output.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 10: Switch statement

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

Java puzzle 10: A switch statement

Level: Normal

What is the output of this program and why?

public class MySwitch
{
  public static void main(String[] args)
  {
    int x = 2;
    switch(x)
    {
      case 1:
        x += 2;
        break;
      case 2:
        x++;
      case 3:
        x += 4;
      default:
        x += 2;
    }
    System.out.print(x);
  }
}
Author: Sar Maroof

Answer explanation

Answer explanation

  1. The initial value of x is 2. So, case 1 doesn’t match the value of x.
  2. The statement x++; under case 2 is executed and the value of x is incremented by 1, x = 2 + 1 = 3.
  3. There is no break in case 2. Therefore, the statement under case 3 x += 4; is executed and the value of x becomes  x = 3 + 4 = 7.
  4. There is no break statement in case 3. Therefore, the statement x += 2 under default is executed. The value of  x is incremented by 2. x = 7 + 2 = 9.
    The correct answer is 9.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 09: Demonstrating a for loop

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

A loops is one of the most difficult concept for beginner programmers. To master loops you need to practice and parctice again. Therefore, I demonstrate many loops for beginners to solve. This loop is a for loop, which is often used in almost every Java program. You might need a pen and a paper if you can’t memorize the process. That is not necessary for advanced and intermediate programmers.

Java puzzle 9: Demonstrating a for loop

Level: Easy

What is the output of this program and why?

public class ForLoop
{
  public static void main(String[] args)
  {
    int x = 0;
    for(int i = 0; i < 5; i++)
    {
      x += i;
    }
    System.out.print(x);
  }
}
Author: Sar Maroof

Answer explanation

  1. The statement for(int i = 0; i < 5; i++) means repeat the process as long as the variable i is smaller than 5 starting with an initial value of i = 0. After each repetition the value of i is incremented by 1.
  2. According to the info of the step 1, the body of the loop is 5 times executed when i = 0, 1, 2, 3, 4 and after that the execution of the body of the loop is terminated.
  3. The initial value of the variable x is 0. So, the first time the initial value of x = x + i = 0 + 0 = 0.
  4. The second time the value of x becomes x = x + i = 0 + 1 = 1.
  5. The third time the value of x becomes x = x + i = 1 + 2 = 3.
  6. The fourth time the value of x becomes x = x + i = 3 + 3 = 6.
  7. The fifth time the value of x becomes x = x + i = 6 + 4 = 10.

The correct answer is x = 10 and the statement System.out.print(x); writes 10 to the standard output.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 08: Calling constructors within other constructors

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

Sometimes, you need to call a constructor within another constructor. Here is an example of how to do that. Try to use a pen and a paper to write notes if you can’t follow all the procedure in your mind clearly.

What is the output of this program and why?

Java puzzle 8: Calling constructors within other constructors

Level: Hard

public class MyConstructor
{
  String x = "f";
  String y = "g";
  
  MyConstructor()
  {
    this("c");
    System.out.print("a");
  }
  MyConstructor(String str)
  {
    this("d", "e");
    this.x = str;
    System.out.print(x);
  }
  MyConstructor(String str, String str2)
  {
    this.y = str2;
    System.out.print(x + "" + y);
  }
  public static void main(String[] args)
  {
    MyConstructor mc = new MyConstructor();
  }
}
Author: Sar Maroof

Answer explanation

  1. By creating the object mc of MyConstructor, the no-argument constructor of the class is called.
  2. The no-argument constructor calls the one-argument constructor.
  3. The one-argument constructor calls the two-argument constructor.
  4. In the block of the two-argument constructor, the value of the x remains the initial value of x, which is f.
  5. In the block of the two-argument constructor, the value of the parameter str2 is assigned to the instance variable y.
  6. In the block one-argument constructor, we passed the values d and e to the two-argument constructor.
  7. So, the statement System.out.print(x + “” + y); writes fe to the standard output.
  8. The second statement in the block one-argument constructor this.x = str assigns the value of c to the variable x. Therefore the statement System.out.print(x); writes c to the standard output.
  9. At last in the no-argument constructor block the statement System.out.print(“a”); writes the letter a to the standard output.
    The correct answer is that this program writes feca to the standard output.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 13: Working with while loops

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

This program is a simple while loop. Working with while loop is needed regularly for every Java programmers.

Java quiz 13: Working with while loops

Level: Easy

What is written to the standard output as the result of executing the following code?

public class MyClass
{
  public static void main(String[] args)
  {
    int i = 1;
    int x = 1;
    while(i < 4)
    {
      x += i;
      i++;
    }
    System.out.print(x);
  }
}
  Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. The initial value of i is 1. Each time the body of the loop is executed the value of i is incremented by one. The while statement returns true as long as the value of i is smaller than 5.
  2. By the first execution of the loop the value of i is 1, the statement of x += i; increments the value of x by i. The initial value of x is 1. So, x = x + i = 1 + 1 = 2.
  3. By the second execution of the loop the value of i is 2. x = x + i = 2 + 2 = 4.
  4.  By the third execution of the loop the value of i is 3. x = x + i = 4 + 3 = 7.
  5. By the fourth execution of the loop the value of i is 4. x = x + i = 7 + 4 = 11.
  6. By the fifth time the value of i is 5 and that is not smaller than 5. Therefore the loop is terminated and 11 is written to the standard output.

The correct answer is: d.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 12: A sequence of conditional statements

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

Here we have a sequence of conditional statements.

Java quiz 12: A sequence of conditional statements

Level: Normal

What is written to the standard output as the result of executing the following code?

public class Condition
{
  public static void main(String[] args)
  {
    int x = 2;
    String str = "";
    boolean isActive = false;
    if(!isActive)
    {
      str += x;
      x += 3;
      System.out.print(str);
      isActive = true;
    }
    if(isActive)
    {
      str += x;
      System.out.print(str);
    }
  }
}
  Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. The condition if(!isActive) returns true, because the boolean variable isActive is false.
  2. The statement str += x; adds a string value of x to the object str, which is 2.
  3. The statement x += 3; increments the value of x by 3. So, x = 2 + 3 = 5.
  4. The statement System.out.print(str); writes 2 to the standard output.
  5. The statement isActive = true; reassigns the value true to the boolean variable isActive. Therefore the the statement if(isActive) returns true.
  6. The statement str += x; adds the string value of x which is 5 to the initial value of str which is 2.
  7. The statement System.out.print(str); prints the value the string value of str which is 25 to the standard output.
  8. Don’t forget that in the first block the value of str is also writtento the standard output.
  9. The program writes 225 to the standard output.

The correct answer is: f.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 11: Practicing with while loop

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

The while loop is one of the types of loops in Java. Every advanced Java programmer see what happens in this program,  but unfortunately that doesn’t apply to beginners. Therefore, as a beginner programmer, you need to practice regularly to master that kind of loops.

Java quiz 11: Practicing with while loop

Level: Easy

What is written to the standard output as the result of executing the following code?

 
public class WhileLoop
{
  public static void main(String[] args)
  {
    int i = 1;
    while (i < 9)
    {
      i += 2;
      System.out.print(i);
    }
  }
}
                      Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. The statement while (i < 9) means that execute the body of the loop as long as the variable i is smaller than 9.
  2. The initial value of i is equal to 1 and that is smaller than 9. Therefore the body of the loop is executed.
  3. The statement i += 2; increments the value of i by 2. So, i = 1 + 2 = 3. The statement System.out.print(i); writes 3 to the standard outpu.
  4. The value of i is now 3 and that is smaller than 9. Therefore the body of the loop is executed. The statement i += 2; increments the value of i by 2. So, i = 3 + 2 = 5 and 5 is written to the standard output.
  5. The value of i is now 5 and that is smaller than 9. Therefore the body of the loop is executed. The statement i += 2; increments the value of i by 2. So, i = 5 + 2 = 5 and 7 is written to the standard output.
  6. The value of i is now 7 and that is smaller than 9. Therefore the body of the loop is executed. The statement i += 2; increments the value of i by 2. So, i = 7 + 2 = 9 and 9 is written to the standard output.
  7. The value of i is now 9, but 9 is not smaller than 9 that is why the loop is terminated and the program writes 3579 to the standard output.
    The correct answer is: a.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 10: Calling constructor from constructor

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

In this program we call a constructor within another constructor. Remember what happens when you create an object of a class that extends another class.

Java quiz 10: Calling constructor from constructor

Level: Normal

What is written to the standard output as the result of executing the following code?

public class MySuper
{
  MySuper()
  {
    this(2);
    System.out.print("s");
  }
  MySuper(int x)
  {
    System.out.print(x);
  }
}

public class MyClass extends MySuper
{
  MyClass()
  {
    this("t");
    System.out.print("y");
  }
  MyClass(String str)
  {
    System.out.print(str);
  }
  public static void main(String[] args)
  {
    MyClass mc = new MyClass();
  }
}
                                       Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. By creating the object mc, the no-argument constructor of its superclass is called.
  2. The no-argument constructor of the superclass invokes its one-argument constructor by using the statement this(2);.
  3. By passing the value 2 to the one-argument constructor, the value 2 is printed to the standard output.
  4. The statement System.out.print(“s”); prints the letter s to the standard output.
  5. The constructor of MyClass calls its one-argument constructor using the statement this(“t”);
  6. The one-argument constructor of MyClass writes the parameter t to the standard output.
  7. At last the statement System.out.print(“y”); writes y to the standard output.
    So, this program writes 2sty to the standard output.

The correct answer is: e.

 


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 07: Variable types in Java

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

There are different types of variables in Java. Many beginners does not distinguish between them. Here is a new puzzle.

What is the problem in this code?

Java puzzle 7: Variable types in Java

Level: Easy

public class Student
{
  static boolean isForeigner;

  public static void main(String[] args)
  {
    String name = "Mike";
    int age = 22;
    static char gender; // m = male, f = female
    System.out.print(name + " ");
    System.out.print(age + " ");
    System.out.print(isForeigner + " ");
    System.out.print(gender);
  }
}
Author: Sar Maroof

Answer explanation

  1. In this program the class variable isForeigner is not initialized, which is allowed in Java.
  2. Local variables such as name, age and gender in this program must be initialized.
  3. The problem here is the variable gender, which is a local varable and it is not initialized.
  4. You can fix the issue by initializing the variable gender as follows:
    char gender = ‘m’;

 


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 09: How to handle exceptions?

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

As a programmer you need to know what kind of exceptions might occur in your code.
By handling the exceptions, you would avoid problems for the users of your program.
In the following example, passing some parameters to the method myMethod could cause exceptions. Therefore it is necessary to handle the exceptions that might occur.

Java quiz 9: How to handle exceptions?

Level: Hard

What is written to the standard output as the result of executing the following code?

public class MyException
{
  int[] arrayInt = new int[8];
  int nr = 3;

  public void myMethod(int i, int i2)
  {
    try
    {
      arrayInt[i] = i2;
      nr = i / i2;
      System.out.print("s");
    }
    catch (ArithmeticException ae)
    {
      System.out.print("t");
    }
    catch (ArrayIndexOutOfBoundsException ae)
    {
      System.out.print("x");
    }
  }
  public static void main(String[] args)
  {
    MyException me = new MyException();
    me.myMethod(8, 0);
    me.myMethod(7, 0);
  }
}
                                                                Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. After creating the object me, the method myMethod(8, 0) is invoked. The parameter 8 is passed to the statement arrayInt[i] = i2; as follows: arrayInt[8] = 0.
    Since the eighth element of the array doesn’t exist, the exception ArrayIndexOutOfBoundsException is occured.
    Therefore the program writes “x” to the standard output.
  2. When the method myMethod(7, 0) is invoked, the statement nr = i / i2; divides 7 by zero. Dividing by zero causes ArithmeticException. Therefore, the program writes “t” to the standard output.
    So, this program writes xt to the standard output.

The correct answer is: f.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 08: Array elements

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

Here is a simple array of Strings with the length of 5. We assigned the values of all the elements of the array.

If you are not familiar with arrays, you can read my article “Java arrays” before answering this quiz.

Java quiz 8: Array elements

Level: Normal

What is written to the standard output as the result of executing the following code?

import java.util.Arrays;

public class ProgrammingLanguage
{
  public static void main(String[] args)
  {
    String[] lang = new String[5];
    lang[0] = "Java ";
    lang[1] = "C++ ";
    lang[2] = "Ruby ";
    lang[3] = "Perl ";
    lang[4] = "Python ";
    Arrays.sort(lang);
    for (int i = 0; i < lang.length; i++)
    {
      if (i == 2 || i == 4)
      {
        System.out.print(lang[i]);
      }
    }
  }
}
                                               Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. In the beginning the order of the elements of the array pLanguage is
    Java C++ Ruby Perl Python
  2. The statement Arrays.sort(pLanguage); orders the elements of the array alphabetically as follows: C++ Java Perl Python Ruby
  3. The conditional statement if (i == 2 || i == 4) is only true for the second and forth elements. The second element isn’t Java but Perl, because the index of arrays starts from zero. The fourth element is Ruby. Therefore the program writes Perl Ruby to the standard output.

The correct answer is: b.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 06: Controlling a loop

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

In this Java puzzle we try to control the results of a loop.
If you compile and run this program, it writes 123456 to the standard output.
By adding only one statement to the line 9, the program will write 1346 to the
standard output. What is that statement? More than one solution is possible!

Java puzzle 6: Controlling a loop

Level: Normal

public class Loop
{
  public static void main(String[] args)
  {
    for (int i = 1; i < 7; i++)
    {
      if (i == 2 || i == 5)
      {
        // add your statement here!
      }
      System.out.print(i);
    }
  }
}
  Author: Sar Maroof

Answer explanation

By adding the label continue; the program will skip the values 2 and 5 of the variable i.
Therefore, the program writes 1346. Remeber that by programming, there might be different methods to achieve the same goal. Here you can also use the statement i++; to get the same output.

public class Loop
{
  public static void main(String[] args)
  {
    for (int i = 1; i < 7; i++)
    {
      if (i == 2 || i == 5)
      {
        continue;
      }
      System.out.print(i);
    }
  }
}

 


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 07: Working with Arrays

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

Here is a simple array of Strings with the length of 4. We assigned values of some of the elements of the array.
If you are not familiar with arrays, you can read my article “Java arrays” before answering this quiz.

Java quiz 7: Working with Arrays

Level: Normal

What is written to the standard output as the result of executing the following code?

public class MyArray
{
  public static void main(String[] args)
  {
    String[] strArray = new String[4];
    strArray[2] = "Tiger";
    strArray[1] = "Lion";
    strArray[3] = "Jaguar";
    for (int i = 0; i < 4; i++)
    {
      if (i < 2)
      {
        System.out.print(strArray[i] + " ");
      }
    }
  }
}
                                                         Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. We have assigned the values Tiger, Lion and Jaguar to the last three elements of the array. Remember that we didn’t assign any value to the first element of the array which is the element of zero index, strArray[0]. A value of an uninitialized String or object is null. That is why the value of strArray[0] = null.
  2. The second element of the array is strArray[1], but we initialized the third one first, which is strArray[2] = “Tiger”.
    The order of the elements works according to the index value not which one you assign first.
    The order of the elements is as follows:
    a. strArray[0] = null, because it is not assigned to any values.
    b. strArray[1] = “Lion”.
    c. strArray[2] = “Tiger”.
    d. strArray[3] = “Jaguar”.
  3. The statement if(i < 2) returns true only for the first and the second elements, because their indexes are 0 and 1 and they are smaller than 2. The first two elements of the array strArray are null and Lion. Therefore the program writes null, Lion to the standard output.

The correct answer is: e.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java assignment 04: Working with the class String

Choose an Assignment:    Green = Easy,   Blue = Normal,   Red = Hard
A01  |   A02  |   A03  |   A04  |  

As a Java programmer you will often need to work with the Java standard API specification. I tried to write a Java assignment based on one of my favorite quotes.

The text of the quote2 is a quote by Mahatma Gandhi, but there is an unwanted web site link inside the quote.
Write only one statement on the line 8 to remove that link from the quote. As the result of that the program writes the quote correctly to the standard output as follows:
“Be the change you wish to see in the world.” 

Hint: Search the class String and read what you can do with the method replaceAll in the Java standard API specification.

Java assignment 4: Working with the class String

Level: Normal

public class HandlingText
{
  public static void main(String[] args)
  {
    String quote = "";
    String quote2 = "Be the change you link.com" +
        " wish to see in the world.";
    // add your statement here!
    System.out.print(quote);
  }
}
 Author: Sar Maroof

 

Answer explanation

You can use the method replaceAll.
Remember that by programming there might be many ways to achieve a specific goal.
The statement is: quote = quote2.replaceAll(“link.com “, “”);. The website link will be replaced by nothig or it will be simply removed. Open the Java standard API specification. again, try to find the method replaceAll(String regex, String replacement) and read more info about what you can do with that method and other methods inside the String class.

 

public class HandlingText
{
  public static void main(String[] args)
  {
    String quote = "";
    String quote2 = "Be the change you link.com" +
        " wish to see in the world.";
    quote = quote2.replaceAll("link.com ", "");
    System.out.print(quote);
  }
}

 


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 06: Handling exceptions

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

Handling Exceptions is one of the important concepts in Java programming language.
I try to make that concept clear in this simple program.
If you are not familiar with Exceptions, I recommend to read my article “Java exception handling using easy exercises” before trying this quiz.

Java quiz 6: Handling exceptions

Level: Normal

What is written to the standard output as the result of executing the following code?

public class MyException
{
  public static void main(String[] args)
  {
    int x = 0;
    int y = 4;
    try
    {
      System.out.print("a");
      y = y / x;
      System.out.print("b");
    }
    catch (ArithmeticException ae)
    {
      System.out.print("c");
    }
    finally
    {
      System.out.print("d");
    }
  }
}
                                                                            Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. Here is obviously an arithmetic exception occurs, because the statement y = y/x; divides y by 0. In the try block the first statement writes the letter “a” to the standard output, but the statement y = y/x; causes an arithmetic exception. Therefore the rest of the try block will be ignored.
  2. The type of the exception is an arithmetic exception. Therefore the catch block is executed and the statement System.out.print(“c”); writes the letter “c” to the standard output.
  3. If you read the article already, you know that the finally block is always executed. Therefore the statement System.out.print(“d”); writes the letter “d” to the standard output.
    So, the correct answer is option c.

 


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 05: Conditional statements with else if

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

I post three different levels of puzzles, quizzes and assignments to make sure that all the members benefit from my posts. Here is an easy quiz for beginners. To solve this quiz you need to understand the difference between the if sequences and the else if sequences, click here if you need to read my article first.

Java quiz 5: Conditional statements with else if

Level: Easy

What is written to the standard output as the result of executing the following code?

public class MyClass
{
  public static void main(String[] args)
  {
    int a = 4;
    int b = 6;
    if (a < b)
    {
      System.out.print("x");
    }
    else if (b > 5)
    {
      System.out.print("y");
    }
    else
    {
      System.out.print("z");
    }
  }
}

Author: Sar Maroof

Select the correct answer.


Answer explanation

By else if conditional statements, once a condition returns true the rest of the
conditional statements will not be executed.
In this program the first condition if (a < b) returns true. The rest of the blocks will be ignored, regardless of
returning true or false.
The correct answer is: d.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java assignment 03: Converting dollars to euros

Choose an Assignment:    Green = Easy,   Blue = Normal,   Red = Hard
A01  |   A02  |   A03  |   A04  |  

Today, I am gonna teach you to write a simple useful Java program.
This program converts any amounts of dollars to euros, when 1 euro = 1.18 dollar.
If you run the program, a text appears in the standard output
that asks users to enter an amount in dollars.
When the user enters the amount and press the enter button, the program converts the dollars to euros.
Assignment: write the body of the method convert.

Java assignment 3: Converting dollars to euros

Level: Normal

import java.util.Scanner;

public class Currency
{
  public static double convert(double amount)
  {
    // assignment: write the body of this method.
  }
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter an amount in dollars: ");
    double amount = input.nextDouble();
    System.out.println("In euro: " + convert(amount));
  }
}
/*
Example: output of the amount $150 
----------------------------------
Enter an amount in dollars: 
150
In euro: 127.11
*/
                                                               Author: Sar Maroof

Answer explanation

The method must calculate the amount of euro and returns the result.

import java.util.Scanner;

public class Currency
{
  public static double convert(double amount)
  {
    double euro = amount / 1.18;
    return euro;
  }
  public static void main(String[] args)
  {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter an amount in dollars: ");
    double amount = input.nextDouble();
    System.out.println("In euro: " + convert(amount));
  }
}


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 04: Overriding methods

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

Hi guys, Here is a Java quiz. If you can solve this quiz and understand the right explanation of the answer, you are talented in Java! Of course, I mean before you read the answer explanation below :-).

Java quiz 4: Overriding methods

Level: Hard

What is written to the standard output as the result of executing the following code?

public class MySuper
{
  int a = 4;

  public MySuper()
  {
    myMethod();
  }
  void myMethod()
  {
    a++;
    System.out.print("y" + a);
  }
}
public class MySub extends MySuper
{
  int b = 3;

  void myMethod()
  {
    System.out.print("y" + b);
  }
  public static void main(String[] args)
  {
    MySub mySub = new MySub();
  }
}
Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. The constructor of the superclass invokes the method myMethod, which is overridden
    in the subclass MySub.
  2. The statement System.out.print(“y” + b); in the method myMethod of
    the subclass writes the string “y” and the value of the variable b
    to the standard output.
  3. The constructor of the superclass invokes the method myMethod, which is overridden in the subclass and the method tries to access the varaible b inside the subclass before it is initialized. Therefore the program writes y0 instead of y3
    to the standard output.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 03: The difference between static and instance variables

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

It is important for every Java programmers to understand the difference between class(static) variables and instance variables. Therefore I wrote this quiz, which clarifies those differences.

Java quiz 3: The difference between static and instance variables

Level: Normal

What is written to the standard output as the result of executing the following code?

public class MyClass
{
  int x;
  static int y;

  MyClass(int i)
  {
    x += i;
    y += i;
  }
  public static void main(String[] args)
  {
    new MyClass(2);
    MyClass mc = new MyClass(3);
    System.out.print(mc.x + "," + mc.y);
  }
}
                                                                                      Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. By creating the first object, we pass the parameter value 2 to the constructor.
    The statement x += i; increments the value of x by 2. the variable x is not initialized.
    Therefore, x = 0 + 2 = 2.
    The statement y += i; increments the value of y by 2. The initial value of y is 0, therefore y = 0 + 2 = 2.
  2. By creating the second object mc, we pass the variable 3 as parameter to the constructor.
    The statement x += i; increments the value of x by 3. The variable x is an instance variable, therefore
    its value for the object mc is equal to 3.
    The statement y += i; increments the value of y by 3. The variable y is a static (class) variable and its last
    value was 2. Therefore the value of y becomes 2 + 3 = 5 and that value is the same for all the objects of the class.The correct answer is c. 3, 5.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 02: Simple conditional statements

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

Here is a Java quiz about conditional statements. Look at the program carefully, because there is a difference between a sequences of if statements and a sequences of else if statments. If you don’t know that, please, read my article first by clicking here.

Java quiz 2: Simple conditional statements

Level: Easy

What is written to the standard output as the result of executing the following code?

public class MyClass
{
  public static void main(String[] args)
  {
    int a = 8;
    int b = 12;
    boolean bool = false;

    if (a < b)
    {
      System.out.print("X");
    }
    if (!bool == false)
    {
      System.out.print("W");
    }
    if (a + b == 21)
    {
      System.out.print("Y");
    }
    else
    {
      System.out.print("Z");
    }
  }
}
Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. The conditional statement of the first if(a < b) returns true, because a is smaller than b. Therefore X is printed to the standard output.
  2. The conditional statement if(!bool == false) returns false, because the boolean variable bool is false.
  3. The condition if(a + b == 21) returns false because a + b = 8 + 12 = 20.
  4. The last if statement returns false. Therefore the else block is executed and the statement System.out.print(“Z”); writes the letter “Z” to the standard output.

So, the correct answer is d.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java quiz 01: Passing parameters to constructors

Choose a Quiz:    Green = Easy,   Blue = Normal,   Red = Hard
Q01  |   Q02  |   Q03  |   Q04  |   Q05  |   Q06  |   Q07  |   Q08  |   Q09  |   Q10  |   Q11  |   Q12  |   Q13  |   Q14  |  

Hi guys, I would like to emphasize that understanding constructors helps you by initializing instance variables. In the following code, I try to make that concept clear. I use a trick to make it difficult to guess the result if you don’t understand the constructor block.

Java quiz 1: Passing parameters to constructors

Level: Normal

What is the output of the following program?

public class Student
{
  String name = "Emily";
  int age = 20;

  Student(String name, int age)
  {
    this.name = name;
    this.age = 22;
  }
  public static void main(String[] args)
  {
    Student st = new Student("Oliver", 21);
    System.out.print(st.name + ", " + st.age);
  }
}
Author: Sar Maroof

Select the correct answer.


Answer explanation

  1. By creating the object st using the constructor of the Student class, we need to pass two parameters to the constructor.
  2. Inside the constructor we assign the name parameter to the class variable name by using the statement this.name = name;. Therefore the name is Oliver.
  3. In the constructor the age parameter is not assigned to the variable age. No matter which age you pass to the constructor, the value 22 is assigned to the age. See the statement this.age = 22;.

The right answer is a. Oliver, 22


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 05: Creating objects

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

In this simple Java puzzle, we create two objects namely empA and empB.
We assigned the name Jack to the variable name of the object empA, and
assigned the name Emma to the variable name of the object empB.

What is the output of this program and why?

Java puzzle 5: Creating objects

Level: Normal

public class Employee
{
  static String name;

  public static void main(String[] args)
  {
    Employee empA = new Employee();
    Employee empB = new Employee();
    empA.name = "Jack";
    empB.name = "Emma";
    System.out.print(empA.name + ", " + empB.name);
  }
}

Author: Sar Maroof

Answer explanation

  1. The objects empA and empB are two different objects, but the variable name is static.
  2. A static member of a class could be a variable or a method and
    it belongs only to the class unlike instance members which belong to the objects.
  3. The last time we assigned the value of the static variable name to Emma.
    That is why this program writes “Emma, Emma” to the standard output.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 04: A simple switch statement

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

If you are or want to become a good programmer in the future and you don’t understand this puzzle series, you got to do something about it.
Every advanced programmer can see most of the results of this series. The third one was very hard, I understand if you ignored it. Programming is writing code not memorizing theories. No employer is intersted in theories, but they ask you to build new features, build a new application or web application. So please, spend some time to understand this puzzle!

This small program is a combination of a switch and a loop.
If the program is compiled and run, it writes “eabcdebcdecdede” to the standard output.
add only one statement to the line 21, as a result of that the program writes “ede” to
the standard output. What is that statement?

Java puzzle 4: A simple switch statement

Level: Normal

public class MyClass
{
  public static void main(String[] args)
  {
    int n = 0;
    while (n < 5)
    {
      n++;
      switch (n)
      {
        case 2:
          System.out.print("a");
        case 3:
          System.out.print("b");
        case 4:
          System.out.print("c");
        case 5:
          System.out.print("d");
        default:
          System.out.print("e");
          // add your statement here
      }
    }
  }
}
                                                                    Author: Sar Maroof

Answer explanation

  1. The condition while n < 5 return true as long as n is smaller than 5.
  2. The initial value of n is 0. Therefore the above mentioned condition returns true.
  3. The statement n++; increments the value of n by one. So, the value of n = 0+1 = 1;
  4. There is no case 1, that is why the statement under the default statement System.out.print(“e”); is executed  and writes e to the standard output.
  5. The statement n+=3; increments the value of n by 3. That means that n = 1 + 3 = 4.
  6. The condition of the loop returns true, because n is still smaller than 5.
  7. The body of the loop is executed and n++; increments the value of n by on. n = 4 + 1 = 5
  8. The statement of the case 5 System.out.print(“d”); writes d to the standard output.
  9. There is no break; statement under case 5. Therefore the statements under the default
    are also executed. The statement System.out.print(“e”); writes e to the standard output.
    The statement n+=3; increments the value of n by 3. So, n = 5 + 3 = 8.
  10. The condition of the loop returns false because n is now greater than 5.
    The loop is terminated.The program writes: “ede” to the standard output.

The answer is: n += 3;

 

public class MyClass
{
  public static void main(String[] args)
  {
    int n = 0;
    while (n &lt; 5)
    {
      n++;
      switch (n)
      {
        case 2:
          System.out.print("a");
        case 3:
          System.out.print("b");
        case 4:
          System.out.print("c");
        case 5:
          System.out.print("d");
        default:
          System.out.print("e");
          n += 3;
      }
    }
  }
}

 


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 03: Inheritance

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

This Java puzzle is a mystery!

By creating an object of the subclass MySub in the main method, the constructor of the superclass MySuper is called.

The constructor of MySuper invokes the method myMethod. The statement System.out.print(“-x” + m);  should write -x3, because the value of m is 3.
The statement System.out.print(“-x” + n); should write -x2, because the value of n is 2.

The mystery is that this program writes -x0 to the standard output.
What is your explanation for that?

Java puzzle 3: Inheritance

Level: Hard

public class MySuper
{
  int m = 3;

  public MySuper()
  {
    myMethod();
  }
  void myMethod()
  {
    System.out.print("-x" + m);
  }
}
public class MySub extends MySuper
{
  int n = 2;

  void myMethod()
  {
    System.out.print("-x" + n);
  }
  public static void main(String[] args)
  {
    MySub mySub = new MySub();
  }
}
Author: Sar Maroof

Answer explanation

  1. The constructor of the superclass invokes the method myMethod, which is overridden
    in the subclass MySub.
  2. The statement System.out.print(“-x” + n); in the method myMethod of
    the subclass MySub writes the string “-x” and the value of the variable n
    to the standard output.
  3. The constructor of the superclass invokes the method myMethod, which is overridden in the subclass and the method tries to access the variable n inside the subclass before it is initialzied. Therefore the program writes -x0 instead of -x2 to the standard output.


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java puzzle 02: Conditional statements

Choose a Puzzle:    Green = Easy,   Blue = Normal,   Red = Hard
P01  |   P02  |   P03  |   P04  |   P05  |   P06  |   P07  |   P08  |   P09  |   P10  |   P11  |   P12  |  

Here is the second simple Java puzzle

If this simple program is compiled and run, it writes the letter “c” to the standard output.
By adding one statement to the line 9, the program writes “ab” to the standard output.
What is that statement?

Java puzzle 2: Conditional statements

Level: Normal

public class MyClass
{
  static int x = 4;
  static int y = 7;
  static int z = 6;

  public static void main(String[] args)
  {
    // add your statement here!
    if (x > y)
    {
      System.out.print("a");
      if (z > y * x && y > 0)
      {
        System.out.print("b");
      }
    }
    else
    {
      System.out.print("c");
    }
  }
}
Author: Sar Maroof

Answer explanation

For the program to write the letter “a” to the standard output, the conditional statement if(x > y) must return true. We can do that either by incrementing the value of x or decrementing the value of y.  By incrementing the value of x, the second conditional statement if( z > y * x) will return false. Therefore, we better decrement the value of y.

If we decrement the value of y by 6, the conditional statement if(z > y * x) will return true.
because z = 6 and y * x = 1 * 4 = 4 and that is smaller than 6.
If we decrement the value of y by 7 or more, the conditional statement if(y > 0) will return false. That is the reason that y should be greater than 0 otherwise the letter b wouldn’t be written to the standard output..

 

public class MyClass
{
  static int x = 4;
  static int y = 7;
  static int z = 6;

  public static void main(String[] args)
  {
    y -= 6; // or y = 1;
    if (x > y)
    {
      System.out.print("a");
      if (z > y * x && y > 0)
      {
        System.out.print("b");
      }
    }
    else
    {
      System.out.print("c");
    }
  }
}

 


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Java assignment 02: Calculate net salary based on tax rate and income

Choose an Assignment:    Green = Easy,   Blue = Normal,   Red = Hard
A01  |   A02  |   A03  |   A04  |  

You can solve this assignment easily if you have learned from the first one.

The method getNetSalary calculates the net salary of employees.
The method applies only if the tax rate is equal to 0.20 and the monthly income is $4000.
Change the code of the method so that it can calculate the
net salary for every desirable combinations of tax rates and monthly incomes.
Please, don’t check out the answer before trying it!

Java assignment 2: Calculate net salary based on tax rate and income

Level: Normal

public class CalculateNetSalary
{
  public double getNetSalary()
  {
    double taxRate = 0.20;
    double tax = 4000 * taxRate;
    double netSalary = 4000 - tax;
    return netSalary;
  }
  public static void main(String[] args)
  {
    CalculateNetSalary cns = new CalculateNetSalary();
    System.out.println(cns.getNetSalary());
  }
}

Author: Sar Maroof

Answer explanation

To answer this assignment you need to pass the tax rate and the monthly income
as parameters of the type of double to the method getNetSalary.
By doing so, you can calculate the net salary for every tax rates and monthly incomes.

Try to pass combinations of tax rates and monthly income to the method of your choice!

if this program is compiled and run, it will write the following to the standard output

2240.0
1760.0
2100.0


public class CalculateNetSalary
{
  public double getNetSalary(double taxRate, double income)
  {
    double tax = income * taxRate;
    double netSalary = income - tax;
    return netSalary;
  }
  public static void main(String[] args)
  {
    CalculateNetSalary cns = new CalculateNetSalary();
    System.out.println(cns.getNetSalary(0.30, 3200));
    System.out.println(cns.getNetSalary(0.20, 2200));
    System.out.println(cns.getNetSalary(0.50, 4200));
  }
}

 


If you have difficulties to understand this question and the explanation, I think that you need to read my book first. It contains 17 chapters of standard Java. Click on the link for more information about the English version of the book Java quizmaster for beginners

Please, leave your questions, feedback and suggestions in the comments below!
Sarmaroof.com offers a practical method to learn and improve your Java skills. It avoids unnecessary long boring theoretical explanations, but it uses many exercises and quizzes.



Sar Maroof's new book
Java quizmaster for beginners (English)

Java quizmaster voor beginners (Dutch)
Sar Maroof is the author of two Java books namely, Java quizmaster for beginners and A guide to build a Java application.
Experience with developing web applications since 2001
Sun Certified JSP as well as EJB
CIW Certified Internet Webmaster
Applied Science in Physics HBO Amsterdam
As well as Bachellor Science in Physics.

 Subscribe To sarmaroof RSS Feeds! (Your Browser)
 Mail This Page To A Friend!
 Bookmark This Page!

Sarmaroof © 2017