CrazyEngineers Archive
Old, but evergreen and popular discussions on CrazyEngineers, presented to you in read-only mode.
@Morningdot Hablu • 16 Nov, 2010 • 1 like
hello friend's,
I think during exam time we all need the collection of the questions that cover all the topics of the programming language.
Not only during exam time it will also helps you to revise the entire subject.
I know most of our CEan's have some collection of programs which cover all the syllabus.
.
I requested all the CEan's to post your collection of programming questions in java which cover some sections.

@Manish Goyal • 16 Nov, 2010 Do you mean theoretical questions or programming based questions?
@Morningdot Hablu • 16 Nov, 2010 Not theoretical....post only your programming question collections....So that all our CEan's can give a try to it.
You have to post the questions that cover the whole concepts of a particular topic.
@Ankita Katdare • 16 Nov, 2010 Read the following program:

public class test {
public static void main(String [] args) {
int x = 3;
int y = 1;
if (x = y)
System.out.println("Not equal");
else
System.out.println("Equal");
}
}

What is the result?
@Ankita Katdare • 16 Nov, 2010 Write java application program for generating 4 threads to do the following operations.
(i) getting n numbers
(ii) printing even numbers
(iii) printing odd numbers
(iv) printing average of a numbers.
@Ankita Katdare • 16 Nov, 2010 Q. Write a java application to get n numbers and print them in the order using Heap sort procedure.

Q. Write a program to implement shallow copy and deep copy.

Q. Assume you have an array list with 7 objects in it. And there is an int[] as:

int[] intArray = new int[3];
intArray[0] = 2;
intArray[1] = 4;
intArray[2] = 6;
How do you go about deleting the objects from ArrayList based on the values in int[]?
@Ankita Katdare • 16 Nov, 2010 Q. Can main() of one program in Java be invoked in another Java Program's main()? Explain with example.

Q. Write a program in Java to give the output as follows:

1
1 2
1 2 3
1 2 3 4

and

1
2 3
4 5 6
7 8 9 10
@Manish Goyal • 16 Nov, 2010
Q. Write a program to implement shallow copy and deep copy
can you please explain about this program?
@Morningdot Hablu • 16 Nov, 2010 Display programs...

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
....
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
......
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
......
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
......
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
.......
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
.......
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
.........
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
.........
............1
.........1 2 1
......1 2 3 2 1
...1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
...........

............1
.........1 2 1
......1 2 3 2 1
...1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
...1 2 3 4 3 2 1
......1 2 3 2 1
.........1 2 1
............1
.
@Manish Goyal • 16 Nov, 2010
AbraKaDabra
Q. Write a program in Java to give the output as follows:

1
1 2
1 2 3
1 2 3 4

and

1
2 3
4 5 6
7 8 9 10
class test {
public static void main(String [] args) {
int n=1;
for(int i=1;i<5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print((n++)+(" "));
}
System.out.println();
}
}
}
class test {
public static void main(String [] args) {
for(int i=1;i<5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
PS:-I love these kinds of program since the day i fell in love with programming 😀
@Manish Goyal • 16 Nov, 2010
mohit007kumar00
Display programs...

1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
....
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
......
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
......
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
......
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
.......
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
.......
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
5 4 3 2 1
.........
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
.........
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
...........

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1
.
Do you want to display this output through a single program?
@Morningdot Hablu • 16 Nov, 2010 No It's the collection of programs.
.
But ya it's also a good idea to display all through a single program....give a try to it.
@Ankita Katdare • 16 Nov, 2010 Re: Collection of programming questions in java?

goyal420
can you please explain about this program?
Java provides a mechanism for creating copies of objects called cloning.
There are basically two ways to make a copy of an object called shallow copy and deep copy.

Shallow copy is a bit-wise copy of an object. A new object is created that has an exact copy of the values in the original object.
If any of the fields of the object are references to other objects, just the references are copied. Thus, if the object you are copying contains references to yet other objects, a shallow copy refers to the same sub objects.

Deep copy
is a complete duplicate copy of an object. If an object has references to other objects, complete new copies of those objects are also made.
A deep copy generates a copy not only of the primitive values of the original object, but copies of all sub objects as well, all the way to the bottom. If you need a true, complete copy of the original object, then you will need to implement a full deep copy for the object.

A little search on the internet will show you how to implement them using a Java program. 😉
@Manish Goyal • 16 Nov, 2010 @akd Thanks for explanation
@mohit hm i will give it a try
@Deepika Bansal • 17 Nov, 2010
AbraKaDabra
Read the following program:

public class test {
public static void main(String [] args) {
int x = 3;
int y = 1;
if (x = y)
System.out.println("Not equal");
else
System.out.println("Equal");
}
}

What is the result?

The result is the compilation error in the code because if(x=y) is invalid in java. It should be if(x==y).
Please correct me if i am wrong.
@Deepika Bansal • 17 Nov, 2010
mohit007kumar00
No It's the collection of programs.
.
But ya it's also a good idea to display all through a single program....give a try to it.
it can be a menu based program. like what option the user chooses, accordingly the program will react.
Is anyone having any other idea to implement these series in a single program code...??
@Manish Goyal • 17 Nov, 2010 yeah deepika this can be made

i am thinking about a code through which you can create any pattern of your choice ,just input no and pattern will be ready
@ritz53 • 07 Jan, 2011 how to write programmme in java.. Odd number of position and even number of pos

write a programme in java ...odd number of position and even no. of position ...
eg: 5684
odd add no: 5+8=13
even add no.:6+4=10
@Varsha0802 • 22 Jan, 2011
AbraKaDabra;
Q. Can main() of one program in Java be invoked in another Java Program's main()? Explain with example.
Can it be done? Please explain. I haven't ever heard of this.
@Varsha0802 • 28 Jan, 2011 • 1 like I got the answer. main() of one program in Java can be invoked in another Java Program's main(). It behaves just like any other method.
@Deepika Bansal • 29 Jan, 2011 varsha can you please quote the example.??
@Varsha0802 • 30 Jan, 2011 Deepika consider the following example:

class sum
{      public static void main(String args[])
     {       String a[]={"5","7"};
                     int c;
                     c=calsum.main(a);
                     System.out.println("Sum:"+c);
          }
}    
[\code]

[code]
class calsum
{        public static int main(String args[])
           {        int a,b;
                      a=Integer.parseInt(args[0]);
                      b=Integer.parseInt(args[1]);
                      return(a+b);
          }
}
[\code]

When you will compile the program it will print the sum. Here main behaves as any other public static method.
@Deepika Bansal • 30 Jan, 2011 Thank you varsha for this example. 😀
@vinci • 16 Jan, 2012
AbraKaDabra
Read the following program:

public class test {
public static void main(String [] args) {
int x = 3;
int y = 1;
if (x = y)
System.out.println("Not equal");
else
System.out.println("Equal");
}
}

What is the result?
incompatible type in if
@BoomBoom • 23 Jul, 2012 Hello people. i do have a question i am stuck with. can anyone help me with that?
The Question is-
Write a program to check whether a number entered by the user is a magic number or not.how do i do this program?
i tried doing it with do-while loop but i guess my condition was wrong.
@Anoop Kumar • 23 Jul, 2012
BoomBoom
Hello people. i do have a question i am stuck with. can anyone help me with that?
The Question is-
Write a program to check whether a number entered by the user is a magic number or not.how do i do this program?
i tried doing it with do-while loop but i guess my condition was wrong.
could you please define what is exactly a magic number? not getting exact definition.
@BoomBoom • 23 Jul, 2012
ianoop
could you please define what is exactly a magic number? not getting exact definition.
a number is said to be a magic number if the sum of it individual digit is 0.
eg- 172=1+7+2=10=1+0=1
@Anoop Kumar • 23 Jul, 2012 I did not get this, output of 172 is still 1.

BTW Solution:
you can get a while loop and do operation like
while(yourNumber>0){

tmp= yourNumber%10; /*modulo operation which returns reminder */
/* add tmp in array */
yourNumber=yourNumber/10;
}

add the all numbers of array and decide.
@nareshkumar6539 • 26 Jul, 2012
BoomBoom
Hello people. i do have a question i am stuck with. can anyone help me with that?
The Question is-
Write a program to check whether a number entered by the user is a magic number or not.how do i do this program?
i tried doing it with do-while loop but i guess my condition was wrong.
package com.naresh;
import java.util.Scanner;
public class MagicNumber {
staticintsum;
static MagicNumber mn=new MagicNumber();
publicstaticvoidmain(String args[])
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter a number");
int n=scan.nextInt();
mn.addDigits(n);
}//End of main()
publicvoid addDigits(int n)
{
sum=0;
while(n>0)
{
sum=sum+n%10;
n=n/10;

}
mn.checkSum(sum);

}//End of addDigits()
publicvoid checkSum(int sum)
{
if(sum>=0&&sum<10)
{
if(sum==0)
{
System.out.println("Given number is magic number");
System.exit(0);
}
else
{
System.out.println("Given number is not a magic number");
System.exit(0);
}
}
else
mn.addDigits(sum);
}//End of checkSum()

}//End of the class
@BoomBoom • 07 Aug, 2012 • 1 like thank you people for helping me.
now I am stuck with 2 questions-
1).Write a program to check whether it is a unique number or not. A unique number is a number which has no leading 0 and no digit is repeated in it.
i have done this program using string, but i am not getting that how do i do it without using String. so please can anyone help?

and the other problem is-

2) What is the problem in the following code that the loop is going on to infinity,i.e.,it's not stopping and not giving the correct output.

import java.util.*;
class Word_Scanner
{
public static void main(String args [])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any String(word or sentence)");
String s=sc.next();
int c=0;
while(sc.hasNext())
{
String ss=sc.next();
System.out.println(ss);
c++;
}
System.out.println("Number of words="+c);
}
}

suppose-
INPUT-MY NAME IS ROCKER
OUTPUT-
MY
NAME
IS
ROCKER
Number of words=4

but it is not giving the number of words. So can anyone please explain why am i encountering this problem?
@Anoop Kumar • 07 Aug, 2012 1) Change number into char array and match it.
2) here is modified code
import java.util.*;
 
class Word_Scanner {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any String(word or sentence)");
        int c = 0;
        while (sc.hasNext()) {
            String ss = sc.next();
            System.out.println(ss);
            c++;
 
            if (sc.findInLine(" ") == null) {
                sc.close();
                break;
            }
        }
 
        System.out.println("Number of words=" + c);
    }
}
@Uzair Akmal • 07 Aug, 2012 Java program that will use a counting loop and ask the user for 10 integer values and print the multiplication of the odd numbers which are greater than 1 and less than or equal to 11.
@BoomBoom • 08 Aug, 2012 Can,t we do it directly in the int datatype only without type casting it??
and for the second one-
but what is the problem if we use the line-
String s=sc.nextLine();
This line has no work in the program and using this line won't affect the program also, as we are not using the variable s anywhere, but still it is not showing the correct output. why? can you please explain
@BoomBoom • 08 Aug, 2012
Uzair Akmal
Java program that will use a counting loop and ask the user for 10 integer values and print the multiplication of the odd numbers which are greater than 1 and less than or equal to 11.

import java.io.*;
class odd
{
public static void main(String args [])throws IOException
{
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("Enter 10 integer values");
int c=1;
int p=1;
while(c!=10)
{
c++;
int n=Integer.parseInt(br.readLine());
if(n%2!=0&&n>1&&n<=11)
p=p*n;
}
System.out.println("Product of odd numbers >1 and <=11 ="+p);
}
}
@BoomBoom • 08 Aug, 2012
ianoop
1) Change number into char array and match it.
2) here is modified code
import java.util.*;
 
class Word_Scanner {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter any String(word or sentence)");
        int c = 0;
        while (sc.hasNext()) {
            String ss = sc.next();
            System.out.println(ss);
            c++;
 
            if (sc.findInLine(" ") == null) {
                sc.close();
                break;
            }
        }
 
        System.out.println("Number of words=" + c);
    }
}

Can,t we do it directly in the int datatype only without type casting it??
and for the second one-
but what is the problem if we use the line-
String s=sc.nextLine();
This line has no work in the program and using this line won't affect the program also, as we are not using the variable s anywhere, but still it is not showing the correct output. why? can you please explain sir
@pawan.kumar123 • 08 Aug, 2012
AbraKaDabra
Read the following program:

public class test {
public static void main(String [] args) {
int x = 3;
int y = 1;
if (x = y)
System.out.println("Not equal");
else
System.out.println("Equal");
}
}

What is the result?
this program will not compile because if (x = y) in this x=y returns the new assigned value to x i.e. 1 which is an integer not a boolean resulting in compilation error
@hare singh nayak • 09 Aug, 2012
mohit007kumar00
Not theoretical....post only your programming question collections....So that all our CEan's can give a try to it.
You have to post the questions that cover the whole concepts of a particular topic.
@Anoop Kumar • 09 Aug, 2012
BoomBoom
Can,t we do it directly in the int datatype only without type casting it??
and for the second one-
but what is the problem if we use the line-
String s=sc.nextLine();
This line has no work in the program and using this line won't affect the program also, as we are not using the variable s anywhere, but still it is not showing the correct output. why? can you please explain sir
you can do this using modulo operator. iterate with number%10 and put numbers into set to verify.
you are using System.in that mean you program is reading in infinite loop.
@sookie • 10 Aug, 2012 Write an optimized program to find the largest and second largest no in an array of numbers without using sorting.
@BoomBoom • 10 Aug, 2012
ianoop
you can do this using modulo operator. iterate with number%10 and put numbers into set to verify.
you are using System.in that mean you program is reading in infinite loop.
Sorry but can you please elaborate on that point of my loop being infinite because i am using System.in ??
@Anoop Kumar • 10 Aug, 2012
BoomBoom
Sorry but can you please elaborate on that point of my loop being infinite because i am using System.in ??
System.in will continue to read from command line as you have condition while(sc.hasNext)..
there will always be next thing in case of any key stroke. and if there is no key stroke wait for it.
@hare singh nayak • 12 Aug, 2012 what is output of this program and please explain in detail..


#include<stdio.h>
main()
{
int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} };
int *p,*q;
p=&a[2][2][2];
*q=***a;
printf("%d----%d",*p,*q);
}
@crystal m • 13 Aug, 2012 public class triangle {
public static void main(String[] args)
{
int i=0,count=0;
int num=1;
while(num<6)
{
for(i=0;i<num;i++)
{
System.out.print(num-(count-i)+" ");
}
num++; count++;
System.out.println();

}
}
1.4k views

Related Posts

@Kaustubh Katdare · May 2, 2010

Popular Indian Author of "Stay Hungry, Stay Foolish", Rashmi Bansal is back with her latest novel, "Connect The Dots". The first book featured stories of entrepreneurs who had a degree...
2.5k views

@Ankita Katdare · Aug 23, 2012

Renewable is not only the need of the hour, but also the talk of the town. And Engineering students are opting for the green when it comes to their B.E./B.Tech...
104.7k views

@avii · Feb 23, 2012

Lately I have been reading lots of experiences working in Indian IT companies. All of these are service based & which are majority in taking up grads in campus jobs...
10.6k views

@Kaustubh Katdare · Jul 18, 2008

The concept of Paperless Office is well known. Do you think the world will be completely paperless by 2050? At least, do you think we'll still be using papers in...
1.2k views

@Saandeep Sreerambatla · Aug 12, 2013

Hi All, While commuting to office I usually listen to my radio in my car. My office is close to CM's (chief Minister's) office. So very often they roam here...
1.2k views