6 Replies - 212 Views - Last Post: 30 January 2016 - 02:12 AM

#1 King Hebes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-January 16

Java Exercises and Questions (PS. Proofreader wanted)

Posted 29 January 2016 - 03:01 AM

Hello all. I am new to computer programming and am currently teaching myself the Java language. I am reading a book and at the end there are some questions and exercises and programming activities. I will post the questions and exercises here for others to try if they would like to test their own knowledge. I would also be appreciative if some people with great Java understanding would go over my own answers and let me know how I am doing. Unfortunately this book does not provide answers to all of the exercises so I do not know if some answers are correct or not and I would hate to be learning untrue things. If people like this and are interested I can post chapter 1 and can continue to post the next chapters as I complete them.


Self-Review Chapter 2

2.1 What are primitive data? How are primitive data types different from objects?
Spoiler


2.2 What is a string literal?
Spoiler


2.3 What is the difference between the print method and the println method?
Spoiler


2.4 What is a parameter?
Spoiler


2.5 What is an escape sequence? Give some Examples.
Spoiler


2.6 What is a variable declaration?
Spoiler


2.7 How many values can be stored in an integer variable at one time?
Spoiler


2.8 What are the four integer data types in Java? How are they different?
Spoiler


2.9 What is a character set?
Spoiler


2.10 What is operator precedence?
Spoiler


2.11 What is the result of 19%5 when evaluated in a Java expression? Explain.
Spoiler


2.12What is the result of 13/4 when evaluated in a Java expression? Explain.
Spoiler


2.13 If an integer variable diameter currently holds the value 5, what is its value after the following statement is executed? Explain.
diameter = diameter *4;
Spoiler


2.14 If an integer variable weight currently holds the value 100, what is its value after the following statement is executed? Explain.
weight -= 17;
Spoiler


2.15 Why are widening conversions safer than narrowing conversions?
Spoiler


Exercises

2.1 Explain the following programming statement in terms of objects and the services they provide.
System.out.println (“I gotta be me!”) ;
Spoiler


2.2 What output is produced by the following code fragment? Explain.
System.out.print (“Here we go! ”) ;
System.out.println (“12345”) ;
System.out.print (“Test this if you are not sure. ”) ;
System.out.print (“Another.”) ;
System.out.println () ;
System.out.println (“All done.”) ;
Spoiler


2.3 What is wrong with the following program statement? How can it be fixed?
System.out.println (“To be or not to be, that is the question.”) ;
Spoiler


2.4 What output is produced by the following statement? Explain.
System.out.println (“50 plus 25 is “ + 50 + 25) ;
Spoiler


2.5 What output is produced by the following statement? Explain.
System.out.println (“He thrusts his fists\n\tagainst” +
“ the post\nand still insists\n\the sees the \”ghost\””) ;

Spoiler


2.6 What value is contained in the integer variable size after the following statements are executed?

size = 18 ;
size = size + 12 ;
size = size * 2 ;
size = size / 4 ;
Spoiler


2.7 What value is contained in the floating point variable depth after the following statements are executed?
depth = 2.4 ;
depth = 20 – depth * 4 ;
depth = depth / 5 ;
Spoiler


2.8 What value is contained in the integer variable length after the following statements are executed?
length = 5 ;
length *= 2 ;
length *= length ;
length /= 100 ;
Spoiler


2.9 Write four different program statements that increment the value of an integer variable total.
Spoiler


2.10 Given the following declarations, what result is stored in each of the listed assignment statements?

int iResult, num1 = 25, num2 = 40, num3 = 17, num4 = 5;
double fResult, val1 = 17.0, val2 = 12.78;

a.) iResult = num1 / num4;
Spoiler

b.) fResult = num1 / num4;
Spoiler

c.) iResult = num3 / num4;
Spoiler

d.) fResult = num3 / num4;
Spoiler

e.) fResult = val1 / num4;
Spoiler

f.) fResult = val1 / val2;
Spoiler

g.) iResult = num1 / num2;
Spoiler

h.) fResult = (double) num1 /num2;
Spoiler

i.) fResult = num1 / (double) num2;
Spoiler

j.) fResult = (double) (num1 / num2);
Spoiler

k.) iResult = (int) (val1 / num4);
Spoiler

l.) fResult = (int) (val1 / num4);
Spoiler

m.) fResult = (int) ((double) num1 / num2);
Spoiler

n.) iResult = num3 % num4;
Spoiler

o.) iResult = num2 % num3;
Spoiler

p.) iResult = num3 % num2;
Spoiler

q.) iResult = num2 % num4;
Spoiler


2.11 For each of the following expressions indicate the order in which the operators will be evaluated by writing a number beneath each operator.
a.) a – b – c – d
Spoiler

b.) a – b + c – d
Spoiler

c.) a + b / c / d
Spoiler

d.) a + b / c * d
Spoiler

e.) a / b * c * d
Spoiler

f.) a % b / c * d
Spoiler

g.) a % b % c % d
Spoiler

h.) a – (b – c) – d
Spoiler

I.) (a – (b – c)) – d
Spoiler

j.) a – ((b – c) – d)
Spoiler

k.) a % (b % c) * d * e
Spoiler

l.) a + (b – c) * d – e
Spoiler

m. (a + B ) * c + d * e
Spoiler

n. (a + B ) * (c / d) % e
Spoiler


Is This A Good Question/Topic? 0
  • +

Replies To: Java Exercises and Questions (PS. Proofreader wanted)

#2 King Hebes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-January 16

Re: Java Exercises and Questions (PS. Proofreader wanted)

Posted 29 January 2016 - 03:11 AM

Ah, bummer! I cannot edit until 16 posts! Well at least I didn't make many mistakes! I can improve formatting a bit once I am able to edit. But Exercise 2.3 I am unable to find anything wrong with the statement. Does anyone know the answer there?
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2290
  • View blog
  • Posts: 4,807
  • Joined: 11-December 07

Re: Java Exercises and Questions (PS. Proofreader wanted)

Posted 29 January 2016 - 04:21 AM

The quotes are wrong, but that might just be your formatting. It's possible you inadvertently corrected the mistake when typing it out.
Was This Post Helpful? 0
  • +
  • -

#4 King Hebes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-January 16

Re: Java Exercises and Questions (PS. Proofreader wanted)

Posted 29 January 2016 - 01:07 PM

View Postcfoley, on 29 January 2016 - 04:21 AM, said:

The quotes are wrong, but that might just be your formatting. It's possible you inadvertently corrected the mistake when typing it out.


I don't see it. It looks just like any other println statement. Perhaps it is a mistake in the book. I've seen a few typos so far and it's only chapter 2.

I'm also unsure if exercise 2.11 is correct.

2.11 n. Woops. Really need that edit function!
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2290
  • View blog
  • Posts: 4,807
  • Joined: 11-December 07

Re: Java Exercises and Questions (PS. Proofreader wanted)

Posted 29 January 2016 - 01:30 PM

You can check that one yourself by putting the expression in a println.
Was This Post Helpful? 0
  • +
  • -

#6 King Hebes  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 29-January 16

Re: Java Exercises and Questions (PS. Proofreader wanted)

Posted 30 January 2016 - 01:12 AM

View Postcfoley, on 29 January 2016 - 01:30 PM, said:

You can check that one yourself by putting the expression in a println.


By putting that equation in a println I am not informed of the order of precedence. The parenthesis will execute first and regardless of which is executed first the answer will be the same. But I do not know which will happen first. I would think that parenthesis would execute from left to right, but the operators involved could factor in I suppose.
Was This Post Helpful? 0
  • +
  • -

#7 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2290
  • View blog
  • Posts: 4,807
  • Joined: 11-December 07

Re: Java Exercises and Questions (PS. Proofreader wanted)

Posted 30 January 2016 - 02:12 AM

OK then do an experiment. Change the values of the variables so that the order of precedence will make a difference. Then run the program and see which is true.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1