Welcome to TRiBot Forums

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Sign in to follow this  
Followers 0
baileyr12

Need more Java Help...

8 posts in this topic

I am really stuggling to keep up in my computer science class, and i am going to tutoring today. But these are the couple questions on my homework i cant figure out...

 

 

1. State whether the following statements are true or false:

  • ArrayList index values are integers.
  • ArrayList can change their size, getting larger or smaller.
  • Arrays can change their size, getting larger or smaller.
  • ArrayLists can only hold object types.
  • If you set an element of an array to null, the array reduces its size by 1.
  • If you pass an ArrayList to a method, what is actually passed is a reference to the ArrayList, so any changes you make to the list will be seen by the calling routine.
  • If you pass an Array to a method, a reference is passed, and not a copy is made.
  • Two elements of an array of String can point to the same string.
  • Arrays, ArrayLists, and Strings all use zero-based indexing in Java.

2. Using Java's built-in sorting routines, write a line of code that will sort the array below.

 

int q[] = ...

 

3. Using Java's built-in sorting routines, write one line of code that will sort the ArrayList of strings below.

 

ArrayList<String> z = ...

 

4. The following code segments have errors. Rewrite the code in each example to fix the error.

  1. ArrayList<> vals = new ArrayList<Integers>();
  2. ArrayList(Double) vals = new<> ArrayList ()

5. Rewrite the following code using the enhanced for loop construct. Assume that list is of type ArrayList<Double>.

 

double squareProduct = 1.0

for (int i=0; i<list.size(): i++)

{

     squareProduct = squareProduct * list.get(i) * list.get(i);

}

System.out.println(squareProduct);

 

 
Edited by baileyr12

Share this post


Link to post
Share on other sites
  • ArrayList index values are integers. true, i.e. myArrayList.get(0) 
  • ArrayList can change their size, getting larger or smaller. true, arraylists are dynamic
  • Arrays can change their size, getting larger or smaller. false, arrays are of static length
  • ArrayLists can only hold object types. false, they hold references to objects
  • If you set an element of an array to null, the array reduces its size by 1. false
  • If you pass an ArrayList to a method, what is actually passed is a reference to the ArrayList, so any changes you make to the list will be seen by the calling routine. true
  • If you pass an Array to a method, a reference is passed, and not a copy is made.  false, arrays are passed by value not reference
  • Two elements of an array of String can point to the same string. true
  • Arrays, ArrayLists, and Strings all use zero-based indexing in Java. true

Should be all good as long as I remember my CS classes well :P

Share this post


Link to post
Share on other sites

 

  • ArrayList index values are integers. true, i.e. myArrayList.get(0) 
  • ArrayList can change their size, getting larger or smaller. true, arraylists are dynamic
  • Arrays can change their size, getting larger or smaller. false, arrays are of static length
  • ArrayLists can only hold object types. false, they hold references to objects
  • If you set an element of an array to null, the array reduces its size by 1. false
  • If you pass an ArrayList to a method, what is actually passed is a reference to the ArrayList, so any changes you make to the list will be seen by the calling routine. true
  • If you pass an Array to a method, a reference is passed, and not a copy is made.  false, arrays are passed by value not reference
  • Two elements of an array of String can point to the same string. true
  • Arrays, ArrayLists, and Strings all use zero-based indexing in Java. true

Should be all good as long as I remember my CS classes well :P

 

Actually, ArrayLists can only hold objects, denoted by the generic type of the ArrayList.

 

Also, no type other than primitives are passed by value in Java. Objects references are passed by value, so the answer to question 7 is false because a copy is made, however the array itself is not passed by value.

Edited by Starfox

Share this post


Link to post
Share on other sites

If you keep asking for someone to do it for you, you're not learning yourself. You need to sit down and figure it out for ask the teacher for help so you can learn it and understand it. The community giving you the answers to turn into the teacher will not help you and will make you just fall behind more.

Share this post


Link to post
Share on other sites

1. State whether the following statements are true or false:

See above (note Starfox' correction on number 4)

 

2. Using Java's built-in sorting routines, write a line of code that will sort the array below.

int q[] = new int[] {4, 13, 9, 24};

Arrays.sort(q);

//q: 4, 9, 13, 24

 

3. Using Java's built-in sorting routines, write one line of code that will sort the ArrayList of strings below.

ArrayList<String> z = new ArrayList();

z.add("bbbb");

z.add("aaaa");

z.add("cccc");

Collections.sort(z);

//z: "aaaa", "bbbb", "cccc"

 

4. The following code segments have errors. Rewrite the code in each example to fix the error.

  1. ArrayList<Integer> vals = new ArrayList<Integer>();
    Note, from JDK7 onwards you can use the diamond operator, this allows you to do this:
    ArrayList<Integer> vals = new ArrayList<>();
     
  2. ArrayList<Double> vals = new ArrayList<Double>()

 

5. Rewrite the following code using the enhanced for loop construct. Assume that list is of type ArrayList<Double>.

double squareProduct = 1.0

for (Double d : list)
{

   squareProduct = squareProduct * d * d
}

System.out.println(squareProduct);

OR

 

double squareProduct = 1;

for (Double d : list)

   squareProduct *= Math.pow(d, 2);

System.out.println(squareProduct);

 

OR

Using Java8:

 

double squareProduct = 1;

list.forEach(d -> squareProduct *= Math.pow(d, 2));

System.out.println(squareProduct);

Share this post


Link to post
Share on other sites

Actually, ArrayLists can only hold objects, denoted by the generic type of the ArrayList.

 

Also, no type other than primitives are passed by value in Java. Objects references are passed by value, so the answer to question 7 is false because a copy is made, however the array itself is not passed by value.

Are you sure on #4? Yes ArrayList can only hold objects (which is why you use Integer instead of primitive int), but they don't hold the objects themselves. They just hold the memory location of said object. Could just be reading between the lines too much.

Share this post


Link to post
Share on other sites

Are you sure on #4? Yes ArrayList can only hold objects (which is why you use Integer instead of primitive int), but they don't hold the objects themselves. They just hold the memory location of said object. Could just be reading between the lines too much.

 

A list indeed holds the reference to the objects, but I think you go a little too much in detail with that answer, especially since OP has troubles keeping up with more basic matter!

Share this post


Link to post
Share on other sites

Are you sure on #4? Yes ArrayList can only hold objects (which is why you use Integer instead of primitive int), but they don't hold the objects themselves. They just hold the memory location of said object. Could just be reading between the lines too much.

Collections like that do hold a reference to the object, but the question asked the type that they hold, so the answer would be true.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.