random(),Java, C ,C++, ASP, ASP.net C# ,Struts ,Questions & Answer, Struts2, Ajax, Hibernate, Swing ,JSP , Servlet, J2EE ,Core Java ,Stping, VC++, HTML, DHTML, JAVASCRIPT, VB ,CSS, interview ,questions, and answers, for,experienced, and fresher
random()

The random() method returns a random double that is greater than or equal to 0.0 and less than 1.0. The random() method does not take any parameters : 
for example :

public class RandomTest {
public static void main(String [] args) {
for (int x=0; x < 15; x++)
System.out.print( (int)(Math.random()*10) + \" \" );
}
}

The println() method multiplies the result of the call to Math.random() by 10, and then casts the resulting double (whose value will be between 0.0 and 9.99999999), to an integer.
Here are some sample results :

6 3 3 1 2 0 5 9 3 5 6 6 0 3 5
4 9 3 6 6 8 1 1 3 0 3 2 5 3 4


The signature of the random() method is as follows :

public static double random( )

More interview questions and answers

Given the following, import java.util.*; class myprog { public static void main(String [] args) { Myprog r = new Myprog(); Object o = r.prog(); } Object test() { ............................ ......................... } } which two of the following code fragments inserted at two dotted lines will not compile?

Given the following, import java.util.*; class myprog { public static void main(String [] args) { myprog r = new myprog(); Object o = r.prog(); } Object prog() { ................... ................... } } which two of the following code fragments inserted at two dotted lines will not compile?

Given the following, class Prog { public static Foo p = new Foo(); public static Foo p2; public static Bar y = new Bar(); public static void main(String [] args) { for (int a=0;a <6; a++) { f2 = getFooa(); f2.react(); } 11. } 12. static Foo getFoo(int b) { if ( 0 == b % 2 ) { return p; } else { return y; } } } class Bar extends Foo { void react() { System.out.print("Bar "); } } class Foo { void react() { System.out.print("Foo "); } } what is the result?

Strings Are Immutable Objects

Important Facts About Strings and Memory

Select one line of code when we insert in following code program will not compile: class R4RSCJPTest { public static void main(String args[]) { int i; R4RSCJPTest test = new R4RSCJPTest(); i = test.run(); System.out.println(i); } int run() { //insert code here return j; } }

What result come when we compile and run following code: public class R4RSCJPTest { public static void main(String args[]) { R4RSCJPTest test = new R4RSCJPTest(3); test.R4RSCJPTest(0); } public R4RSCJPTest(int i) //1 { this((long) i); System.out.print(" " + (i *= 2)); } public R4RSCJPTest(long i) //2 { System.out.print(" " + i); } public void R4RSCJPTest(int i) //3 { System.out.print(" No_Argument "); } }

What result come when we try to compile and run following code: class xyz extends abc { static private int xyzCount = 0; public String xName; int xNumber; static synchronized int addxyz() { xName = “My name is xyz” + xyzCount; return xyzCount; } public xyz() { xNumber = addxyz(); } }

Given the following, public class Myprog { public static void main(String [] args) { String a1 = "abc"; String a2 = "def"; String a3 = a2; a2 = "ghi"; System.out.println(a1 + a2 + a3); } } what is the result?

Given the following, String a = "abc"; a.toUpperCase(); String a = a.replace('B', 'b'); b = b + "xyz"; System.out.println(b); what is the result?

Given the following, String a = new String("abc"); b = "xyz"; a = a + b; how many String objects have been created?

Given the following, String x = "Books"; x = x.substring(5,7); char y = x.charAt(1); x = x + y; System.out.println(x); what is the result?

Given the following, String s = "bookpaper"; s.substring(1,7);s = "w" + s; s.append("woo"); System.out.println(s); what is the result?

Given the following, public class Example { public static void main(String [] args) { double values[] = {-2.3, -1.0, 0.25, 4}; int cnt = 0; for (int x=0; x < values.length; x++) { if (Math.round(values[x] + .5) == Math.ceil(values[x])) { ++cnt; } } System.out.println("same results " + cnt + " time(s)"); } } what is the result?

Which of the following are valid calls to Math.max?

What two statements are true about the result obtained from calling Math.random()?

Given the following, public class myprog { public static void main(String [] args) { double value = -9.0; System.out.println( Math.sqrt(value)); } } what is the result?

Given the following, public class Degrees { public static void main(String [] args) { System.out.println( Math.sin(75) ); System.out.println( Math.toDegrees(Math.sin(75) )); System.out.println( Math.sin(Math.toRadians(75) )); System.out.println( Math.toRadians(Math.sin(75) )); } } at what line will the sine of 75 degrees be output?

Given the following, public class Wrapprog { public static void main(String [] args) { Long y = new Long(42); int a = Integer.valueOf("345"); int a2 = (int) Integer.parseInt("345", 8); int a3 = Integer.parseInt(42); int a4 = Integer.parseInt("42"); int a5 = y.intValue(); } } which two lines will cause compiler errors?

Given the following, public class NFE { public static void main(String [] args) { String s = "42"; try { s = s.concat(".5"); double d = Double.parseDouble(s); s = Double.toString(d); int x = (int) Math.ceil(Double.valueOf(s).doubleValue()); System.out.println(x); } catch (NumberFormatException e) { System.out.println("bad number"); } } } what is the result?

Given the following, public class BoolProg { public static void main(String [] args) { Boolean x1 = new Boolean("false"); boolean x2; x2 = x1.booleanValue(); if (!x2) { x2 = true; System.out.print("a "); } if (x1 & x2) { System.out.print("b "); } System.out.println("c"); } } what is the result?

Select one correct line of code that we insert in following code, program compile and run sucessfully: class Base { public Base(int a) { } } public class Test extends Base { public static void main(String arg[]) { Test t = new Test(7); } Test(int a) { super(a); } Test(String str, int a) { this(a); //Write statement here } }

Given the following, public class WrapProg { public static void main(String [] args) { Long y = new Long(42); int a = Integer.valueOf("345"); int a2 = (int) Integer.parseInt("345", 8); int a3 = Integer.parseInt(42); int a4 = Integer.parseInt("42"); int a5 = b.intValue(); } } which two lines will cause compiler errors?

Given the following, public class WrapProg { public static void main(String [] args) { String a = "98.6"; // insert code here } } which three lines inserted independently at line 4 will cause compiler errors?

Given the following, try { Float f1 = new Float("3.0"); int a = f1.intValue(); byte y = f1.byteValue(); double d = f1.doubleValue(); System.out.println(a + y + d); catch (NumberFormatException e) { System.out.println("bad number"); } what is the result?

Given the following, public class Wrapprog { public static void main(String [] args) { int result = 0; short s = 42; Long a = new Long("42"); Long b = new Long(42); Short c = new Short("42"); Short a2 = new Short(s); Integer b2 = new Integer("42"); Integer c2 = new Integer(42); if (a == b) result = 1; if (a.equals(b) ) result = result + 10; if (a.equals(b) ) result = result + 100; if (a.equals(a2) ) result = result + 1000; if (a.equals(c2) ) result = result + 10000; System.out.println("result = " + result); } } what is the result?

What result come when we compile and run following code: class Test { Test(String s) { System.out.println("String will print"); } Test(Object o) { System.out.println("Object will print"); } Test(char c) { System.out.println("character will print"); } } class R4RSCJPTest { public static void main(String arg[]) { Test t=new Test(null); } }

Given the following, public class BoolProg { public static void main(String [] args) { int result = 0; Boolean a1 = new Boolean("TRUE"); Boolean a2 = new Boolean("true"); Boolean a3 = new Boolean("tRuE"); Boolean a4 = new Boolean("false"); if (a1 == a2) result = 1; if (a1.equals(a2) ) result = result + 10; if (a2 == a4) result = result + 100; if (a2.equals(a4) ) result = result + 1000; if (a2.equals(a3) ) result = result + 10000; System.out.println("result = " + result); } } what is the result?

Given the following, public class ObjProg { public static void main(String [] args ) { int result = 0; ObjProg op = new Objprog(); Object o = op; if (o == op) result = 1; if (o != op) result = result + 10; if (o.equals(op) ) result = result + 100; if (oc.equals(o) ) result = result + 1000; System.out.println("result = " + result); } } what is the result?

Which two statements are true about wrapper or String classes?

What result come when we compile and run following code: class Test { Test(String str) { System.out.println("String will print"); } Test(StringBuffer strbuf) { System.out.println("StringBuffer will print"); } Test(Object obj) { System.out.println("Object will print"); } } class R4RSCJPTest { public static void main(String arg[]) { Test t=new Test(null); } }

What result come when you try to compile this code: public int Test (String str) { try { String abc = Str.substring(0, Str.indexOf(‘,’)); return Integer.parseInt(abc); } catch (NumberFormatException e) { System.out.println(“Error in ” + abc); } return –1; }

What result come we compile and run following code: public class Child extends Parent { public static int run(int i) { return 8; } public static void main(String[] args) { Parent p = new Child(); System.out.println(p.run(5)); } } class Parent { public static int run(int i) { return 14; } }

What result come when we compile and run following code: class Parent { public Parent(int i) { i *= 5; System.out.println("Parent " + i); } } public class Child extends Parent { public Child(int i) { super(4); i += 15; System.out.println("Child " + i); } public static void main(String args[]) { Parent p = new Child(5); } }

What result come when we compile and run following code: class Parent { } class Child1 extends Parent { } class Child2 extends Parent { } public class R4RSCJPTest { public static void main(String args[]) { Parent p = new Parent(); Child2 c = (Child2) p; } }

What result come when we compile and run following code: class Parent { } class Child extends Parent { public String run() { String name = "Vivek"; return name; } } public class R4RSCJPTest { public static void main(String args[]) { Parent p = new Child(); System.out.println(p.run()); } }

What result come when we compile and run following code: class Parent { } class Child extends Parent { public String run() { String name = "Vivek"; return name; } } public class R4RSCJPTest { public static void main(String args[]) { Parent p = new Child(); System.out.println(((Child)p).run()); } }

What result come when we compile and run following code: class Parent { } class Child1 extends Parent { } class Child2 extends Parent { } public class R4RSCJPTest { public static void main(String args[]) { Parent p = new Parent(); Child2 c = (Child2) p; } }

Creating New Strings

Important Methods in the String Class

The StringBuffer Class

Important Methods in the StringBuffer Class

Using the Math Class

Methods of the java.lang.Math Class

abs()

ceil()

floor()

max()

min()

random()

round()

sin()

cos()

tan()

sqrt()

toDegrees()

toRadians()

Give me a list of Static Math Methods

Miscellaneous Math Class Facts

Using Wrapper Classes

An Overview of the Wrapper Classes

Creating Wrapper Objects

The Wrapper Constructors

The valueOf() Methods

Using Wrapper Conversion Utilities: xxxValue()

Using Wrapper Conversion Utilities : parseXxx() and valueOf()

toString()

toXxxString() (Binary, Hexadecimal, Octal)

Using equals()

Comparing Variables : (==)

Comparing Objects

The equals() Method Revealed

Overriding hashCode() and equals()

The toString() Method

Overriding equals()

What It Means if we Don\'t Override equals()

Implementing an equals() Method

The equals() Contract

Overriding hashCode()

Implementing hashCode()

The hashCode() Contract

Collections

So What Do we Do with a Collection?

Key Interfaces and Classes of the Collections Framework

Sorted

Ordered

List

ArrayList

Vector

LinkedList

Set

HashSet

LinkedHashSet

TreeSet

Map

HashMap

Hashtable

LinkedHashMap

TreeMap

Garbage Collection

R4R provides free online tutolrails ,examples for Freshers and experts .We cover core java, tutorials, spring, Hibernate, python, struts, struts 2,c language, spring boot,PHP,MYSQL,C#,ASP.NET,WPF,WCF,Photoshop , JSP, programming, example, Interview questions and answer