Tag Archives: int

Given the code fragment: String name = "Spot"; int age = 4; String str ="My dog " + name + " is " + age;

Given the code fragment:String name = “Spot”;int age = 4;String str =”My dog ” + name + ” is ” + age;System.out.println(str);AndStringBuilder sb = new StringBuilder();Using StringBuilder, which code fragment is the best potion to build and print the followingstring My dog Spot is 4 A. sb.append(“My dog “).append( name ).append(” is ” ).append(age); System.out.println(sb); B. sb.insert(“My dog… Read More »

Given: class Overloading { int x(double d) { System.out.println("one"); return 0; } String x(double d)

Given:class Overloading {int x(double d) {System.out.println(“one”);return 0;}String x(double d) {System.out.println(“two”);return null;}double x(double d) {System.out.println(“three”);return 0.0;}public static void main(String[] args) {new Overloading().x(4.0);}}What is the result? A. Three B. One C. Two D. Compilation fails. Correct Answer: D

Given the code fragment: int[] array = {1, 2, 3, 4, 5}; And given the requirements: 1. Process all the

Given the code fragment:int[] array = {1, 2, 3, 4, 5};And given the requirements:1. Process all the elements of the array in the order of entry.2. Process all the elements of the array in the reverse order of entry.3. Process alternating elements of the array in the order of entry.Which two statements are true? A. Requirements 2 and… Read More »

Given the code fragment: int b = 3; if ( !(b > 3)) { System.out.println("square "); }{ System.out.println("circle

Given the code fragment:int b = 3;if ( !(b > 3)) {System.out.println(“square “);}{System.out.println(“circle “);}System.out.println(“…”);What is the result? A. circle… B. square… C. Compilation fails. D. squarecircle… Correct Answer: D

What is the proper way to defined a method that take two int values and returns their sum as an int value?

What is the proper way to defined a method that take two int values and returns their sum as an int value? A. void sum (int first, int second) { return first + second; } B. sum(int first, int second) { return first + second; } C. int sum(int first, second) { return first + second; } D.… Read More »

Given: class Test int a1; public static void doProduct(int a) { a = a * a; ) public static void doString(StringBuilder

Given:class Testint a1;public static void doProduct(int a) {a = a * a;)public static void doString(StringBuilder s) {s.append(” ” + s);}public static void main(String[] args) {Test item = new Test();item.a1 = 11;StringBuilder sb = new StringBuilder(“Hello”);Integer i = 10;doProduct(i);doString(sb);doProduct(item.a1);System.out.println(i + ” ” + sb + ” ” + item.a1);}}What is the result? A. 100 Hello 121 B. 100 Hello… Read More »

Given: public class TestLoop { public static void main(String[] args) { int array[] = {0, 1, 2, 3, 4};

Given:public class TestLoop {public static void main(String[] args) {int array[] = {0, 1, 2, 3, 4};int key = 3;for (int pos = 0; pos < array.length; ++pos) {if (array[pos] == key) {break;}}System.out.print(“Found ” + key + “at ” + pos);}}What is the result? A. Found 3 at 2 B. Found 3 at 3 C. Compilation fails D. An… Read More »