Looking for a Tutor Near You?

Post Learning Requirement »
x

Choose Country Code

x

Direction

x

Ask a Question

x

x
x
x
Hire a Tutor

Java Find The Output

Loading...

Published in: Java And J2EE
9,929 Views

Some Core Java find the output programs for ICSE class 10 studens.

Agarwal T / Guwahati

year of teaching experience

Qualification:

Teaches: Advanced Excel, Basic Computer, Computer for official job, MS Office, School Level Computer, Computer Science, IT & Computer Subjects, B.Sc Tuition, BCA Tuition, M.Sc Tuition, M.Tech Tuition, DBMS & RDBMS, Oracle Training, PL/SQL, .Net, C / C++, C# (C Sharp), Java And J2EE, Visual Basic, MCA Subjects, Software Testing, Unix/Linux, Digital Marketing

Contact this Institute
  1. Program 1 // filename Main.java class Test { protected int x, y; class Main { public static void main(String args[]) { Test t = new Test(); System.out.println(t.x + " " + t.y); Output 00 In Java, a protected member is accessible in all classes of same package and in inherited classes of other packages. Since Test and Main are in same package, no access related problem in the above program. Also, the default constructors initialize integral variables as 0 in Java (See this GFact for more details). That is why we get output as 0 0. Program 2 // filename Test.java class Test { public static void main(String[] args) { for(int i = 0; 1; i++) { System.out.println("Hello"); break; Output: Compiler Error There is an error in condition check expression of for loop. Java differs from C++(or C) here. C++ considers all non-zero values as true and 0 as false. Unlike C++, an integer value expression cannot be placed where a boolean is expected in Java. Following is the corrected program. // filename Test.java class Test { public static void main(String[] args) { for(int i = 0; true; i++) { System.out.println("Hello"); break;
  2. // Output: Hello Program 3 // filename Main.java class Main { public static void main(String args[]) { System.out.println(fun()); int fun() { return 20; Output: Compiler Error Like C++, in Java, non-static methods cannot be called in a static method. If we make fun() static, then the program compiles fine without any compiler error. Following is the corrected program. // filename Main.java class Main { public static void main(String args[]) { System.out.println(fun()); static int fun() { return 20; // Output: 20 Program 4 // filename Test.java class Test { public static void main(String args[]) { System.out.println(fun()); static int fun() { static int x= 0; return ++x;
  3. Output: Compiler Error Unlike C++, static local variables are not allowed in Java. See this GFact for details. We can have class static members to count number of function calls and other purposes that C++ local static variables serve. Following is the corrected program. class Test { private static int x; public static void main(String args[]) { System.out.println(fun()); static int fun() { return ++x; // Output: I Writing code in comment? Please use code.geeksforgeeks.org, generate link and share the link here. Wasif mport java.io. * class Lcm public static void main(String args[])throws IOException DatalnputStream ins=new DatalnputStream(System.in); System.out.println("By This Application, You Can Find The Lcm Of Any Three Numbers. "); System.out.println(); System.out.println(); do
  4. System.out.println( "Enter First Number: " ); int a=Integer.parseInt(ins.readLine()); System.out.println( "Enter Second Number: " ); int b=Integer.parseInt(ins.readLine()); System.out.println( "Enter Third Number: " ); int c=Integer.parseInt(ins.readLine()); int p=a*b*c, lcm=0; int m=l; for(int i=l ;i
  5. } while(m!=3); //error showing that cannot find symbol m. please help me where I am doing wrong. sen suppose that you have thefollowing class definitions: public class SuperClass protected int x; private Stringstr; public void print() System.out.println(x+ +Str); public SuperClass() str= x=0; public SuperClass(String s,int a) str=s;
  6. x=a; public class SubClass extends SuperClass private int y; public void print() System.out.println( " SubClass: " +y); super.print(); public SubClass() super(); public SubClass(String s,int a,int b) super( "Hellosuper" ,a+b); Y=b; what is the output of the following java code?
  7. SuperClass superObject=new SuperClass( " This issuperClass " ,2); SubC1ass subObject=new SubC1ass( "DDDDDDD" superObject.print(); subObject.print(); datta class abc int c,a=5;b=10; void sum() int a=100; c=a+b; What is the output? Manan Jhaveri 15 datta c=110 kzmusiclover i am not 100% sure. u should verify with others but i think the answer should be on the lines of: hai Inside super class 10,20 hai Inside super class 30,40 Venki 1. Program 2: The reason behind compilation error, Java is more type safe than C++. The literal I is of type int, and int to boolean implicit conversion is not allowed. C++ also good type safe language, it allows literal integers to boolean conversion due to backward compatibility with c.
  8. 2. Program 3, Sandeep 1. If we make fun() non-static " needs some correction. What will be the output of the program? String x = new String("xyz"); String y = "abc"; How many String objects have been created? A. B. c. D. 2 3 4 5 Answer & Explanation Answer: Option C Explanation: Line I creates two, one referred to by x and the lost String "xyz". Line 2 creates one (for a total of three). Line 3 creates one more (for a total of four), the concatenated String referred to by x with a value of "xyzabc". View Answer Workspace Report Discuss in Forum 2. What will be the output of the program? public class WrapTest public static void main(String [ ] args) int result = 0; short s = 42; Long x = new Long("42"); Long y = new Long(42); Short z = new Short("42"); Short x2 = new Short(s); Integer Y2 = new Integer("42");
  9. Integer z2 = new Integer(42); if (x == y) /* Line 13 */ result if (x.equals(y) ) / * Line 15 */ result = result + 10; if (x.equals(z) ) / * Line 17 */ result = result + 100; if (x.equals(x2) ) /* Line 19 */ result = result + 1000; if (x.equals(z2) ) /* Line 21 */ result = result + 10000; System.out.println("result = ' A. B. C. = 11 D. + result); result result result result = 10 = 11010 Answer & Explanation Answer: Option B Explanation: Line 13 fails because == compares reference values, not object values. Line 15 succeeds because both String and primitive wrapper constructors resolve to the same value (except for the Character wrapper). Lines 17, 19, and 21 fail because the equals() method fails if the object classes being compared are different and not in the same tree hierarchy. View Answer Workspace Report Discuss in Forum 3. What will be the output of the program? public class BoolTest public static void main(String [ ] args) int result = 0; Boolean bl = new
  10. Boolean b2 = new Boolean("true"); Boolean b3 = new Boolean("tRuE"); Boolean b4 = new Boolean("false"); if (bl — b2) Line 10 result if (bl.equals(b2) ) / * Line 12 */ result = result + 10; if (b2 == b4) /* Line 14 */ result = result + 100; if (b2.equals(b4) ) / * Line 16 */ result = result + 1000; if (b2.equals(b3) ) / * Line 18 */ A. B. c. D. result = result + 10000; System.out.println("result = ' 0 1 10 10010 + result); Answer & Explanation Answer: Option D Explanation: Line 10 fails because bl and b2 are two different objects. Lines 12 and 18 succeed because the Boolean String constructors are case insensitive. Lines 14 and 16 fail because true is not equal to false. View Answer Workspace Report Discuss in Forum 4. What will be the output of the program? public class ObjComp public static void main(String [ ] args ) int result = 0; ObjComp oc = new ObjComp();
  11. + result); A. B. c. D. Object o = oc; if (o == oc) result if (o != oc) result = result + 10; if (o.equals(oc) ) result = result + 100; if (oc.equals(o) ) result = result + 1000; System.out.println("result = ' 1 10 101 1101 Answer & Explanation Answer: Option D Explanation: Even though o and oc are reference variables of different types, they are both referring to the same object. This means that == will resolve to true and that the default equals() method will also resolve to true. View Answer Workspace Report Discuss in Forum 5. What will be the output of the program? public class Example public static void main(String [ ] args) = {-2.3, -1.0, 0.25, 4}; double values[] int cnt = 0; for (int x=0; x < values.length; x++) if (Math.round(values[x] + .5) == Math.ceil(values[x]))
  12. + cnt + A. B. C. D. +4-cnt•, System.out.println("same results ' same results 0 time(s) same results 2 time(s) same results 4 time(s) Compilation fails. time(s)"); Answer & Explanation Answer: Option B Explanation: Math.round() adds .5 to the argument then performs a floor(). Since the code adds an additional .5 before round() is called, it's as if we are adding 1 then doing a floor(). The values that start out as integer values will in effect be incremented by 1 on the round() side but not on the ceil() side, and the noninteger values will end up equal.